使用 Ajax.Request 将 JSON 从浏览器传递到 PHP 的最佳方法
您好,我有一个 JSON 对象,它是一个二维数组,我需要使用 Ajax.Request 将其传递给 PHP(我知道的唯一方法)。 ...现在我使用js函数手动序列化我的数组...并获取以下格式的数据:s[]=1&d[]=3&[]=4等....
我的问题是:有没有办法更直接/有效地传递 JSON 对象?..而不是自己序列化它?
感谢您的任何建议, 安德鲁
Hi I have a JSON object that is a 2-dimentional array and I need to pass it to PHP using Ajax.Request (only way I know how). ...Right now I manually serialized my array using a js function...and get the data in this format: s[]=1&d[]=3&[]=4 etc. ....
my question is: Is there a way to pass the JSON object more directly/efficientely?..instead of serializing it myself?
Thanks for any suggestions,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 Prototype 的函数 toJSON() 将数组转换为 JSON 对象。 通过 Ajax 调用将其传递到服务器后,只需使用 PHP 函数 json_decode() 即可解码该对象。
You can also use Prototype's function toJSON() to convert an array into a JSON object. After passing it to server via Ajax call, simply use PHP's function json_decode() to decode the object.
将对象作为 JSON 字符串传递给 PHP,并在 PHP 中使用内置 json_decode 从字符串中获取 PHP 对象。
在 Javascript 中,在对象上使用“stringify”函数将其获取为字符串,例如此处提供的库:https://github.com/douglascrockford/JSON-js/blob/master/json2.js
Pass the object as a JSON-string to PHP, and in PHP use the builtin json_decode to get a PHP-object from the string.
In Javascript, use a "stringify" function on your object to get it as a string, library available for example here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
在 Javascript 方面(使用 Prototye):
在 Php 方面:
In que Javascript side (with Prototye):
In que Php side:
查看
http://www.openjs.com/scripts/data/ued_url_encoded_data/
直接正确编码嵌套数据,因为 Object.toQueryString() 不接受嵌套数据...
Check
http://www.openjs.com/scripts/data/ued_url_encoded_data/
to encode nested data directly correct, since Object.toQueryString() doesn't accept nested data...