闭包无法序列化,如何通过AJAX回调到PHP? [英] Closures Can't Be Serialized, How To Do Callbacks via AJAX to PHP?

查看:42
本文介绍了闭包无法序列化,如何通过AJAX回调到PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如何通过AJAX请求的页面调用PHP回调来实现通过AJAX传递的PHP回调?

How can i implement a PHP callback being passed over AJAX with the PHP callback being called by the page requested by AJAX?

设置

评论通过AJAX发布,参数通过序列化和加密传递(因此,在运输过程中不能更改它们,也不能精心设计AJAX滥用评论系统的请求).问题是,与注释本身相比,我需要新的注释总数来更新不同mysql表中的字段(在使用注释的每个位置都会改变).

Comments are posted via AJAX and the paramaters are passed serialized and encrypted (so they can't be changed in transit or carefully crafted AJAX requests to abuse the comments system). The problem is, i need the new total comments amount to update a field in a different mysql table (which will change in every place the comments are used) than what the comments are in themselves.

示例

有人在论坛主题上发表评论,该主题需要知道全部评论(无需每次都知道评论表就查询).问题是,当评论通过AJAX发布时,我们不知道要更新的表,哪些字段等,以及执行其他代码(例如记录所有留下评论的成员)的原因.有关特定主题).

Someone leaves a comment on a forum topic, that topic needs to know the total comments (without querying the comments table everytime this needs to be known). The problem is, when the comments are posted via AJAX we don't know what the table is to update, what fields, etc as well as having to execute additional code along with it (such as also logging all the members who have left comments on the specific topic).

解决方案

我考虑过在声明注释小部件时使用闭包添加两个PHP回调.然后,这两个回调(onSuccess()onFailure())将能够完成所需的工作,例如计算总评论数和更新特定论坛主题的总评论数.然后进行序列化,加密,将其作为参数传递给ajax,然后通过PHP解密并反序列化回调并执行它们.

I thought about adding two PHP callbacks using closures when declaring the comments widget. These two callbacks (onSuccess() onFailure()) would then be able to do what ever work is needed such as counting the total comments and updating the total comments count for the specific forum topic. Then serialize, encrypt it, pass it as a parameter over ajax, then PHP to decrypt and unserialize the callbacks and execute them.

解决方案为何中断

因为闭包无法序列化!另外,我没有在任何人建议之前使用eval.

Because closures can't be serialized! Also, i am NOT using eval before anyone suggests it.

问题再次出现

如何通过AJAX请求的页面调用PHP回调来实现通过AJAX传递的PHP回调?

How can i implement a PHP callback being passed over AJAX with the PHP callback being called by the page requested by AJAX?

更新

似乎有些人没有阅读整个问题,也不了​​解应在PHP中完成回调-而不是javascript(使用AJAX回调).AJAX仅用于传输已发布的注释-不会对其进行处理(这就是PHP所做的,而这正是出现回调的地方).

It would appear that some of you are not reading the whole question and understanding that callbacks are to be done in PHP - NOT javascript (using the AJAX callbacks). AJAX is only used to transport the posted comment - not process it (that's what PHP does and that's where the callback comes in which is the problem).

推荐答案

创建一个作为回调的函数,然后通过ajax传递函数名称,并像这样调用它:

Create a function that is the callback, then pass the function name via ajax, and call it like this:

$callback = 'callback_function_name'; $callback($params...);

这篇关于闭包无法序列化,如何通过AJAX回调到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆