使用 Ajax.Request 将 JSON 从浏览器传递到 PHP 的最佳方法

发布于 2024-07-09 00:07:31 字数 215 浏览 12 评论 0原文

您好,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞清仓 2024-07-16 00:07:31

您还可以使用 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.

゛时过境迁 2024-07-16 00:07:31

将对象作为 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

遇见了你 2024-07-16 00:07:31

在 Javascript 方面(使用 Prototye):

var myJSON= Object.toJSON(youArray);

在 Php 方面:

$myjson = $_POST['myjson'];

$arrayJSON= json_decode(stripslashes($myjson), true);

In que Javascript side (with Prototye):

var myJSON= Object.toJSON(youArray);

In que Php side:

$myjson = $_POST['myjson'];

$arrayJSON= json_decode(stripslashes($myjson), true);
吝吻 2024-07-16 00:07:31

查看
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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文