[英]Passing a PHP object via AJAX?
I am making an AJAX request and I need to send a complete PHP object. 我正在发出AJAX请求,我需要发送一个完整的PHP对象。 This is my JS code:
这是我的JS代码:
new Ajax.Request("some url",
{
method: 'post',
parameters: {
string1:"whatever",
myobject: this is where I want to send a PHP object named $product
}
...
});
Whats the best way? 最好的方法是什么? Maybe using serialization?
也许使用序列化?
Thanks for your help :)! 谢谢你的帮助 :)!
您可以尝试使用json_encode(),通过另一端的请求和json_decode发送
It is not a good idea to send a plain php object. 发送普通的php对象不是一个好主意。 Even when you do something like json encoding.
即使您执行类似json编码的操作。 The problem is that the receiving side might not know about the objects class definition.
问题是接收方可能不知道对象类的定义。
Instead I suggest you add some export/import moethods to your class definition. 相反,我建议您在类定义中添加一些导出/导入方法。 You send the (serialized) result of the export method and import that (unserialized) on the receiving side.
您发送导出方法的(序列化的)结果,并在接收方导入(未序列化的)结果。 That way it is a) clear that you have a valid class on the receiving side and b) you have a place to handle missmatches.
这样一来,可以很清楚地看到:a)在接收端您有一个有效的类,并且b)您有处理错配的地方。 You are more flexible and safer.
您更灵活,更安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.