如何将javascript键值对发送到服务器?

发布于 2024-09-13 16:49:24 字数 196 浏览 0 评论 0原文

我有一个键值对,我想将其发送到服务器。 例如: var obj = {'item1': true, 'item2': false, ........};

我想通过ajax调用将此信息发送到服务器。 但在服务器端我无法获得个人价值。在服务器端,我将“对象”作为字符串获取。 我正在使用 jQuery 进行 ajax 调用。

谁能告诉我该怎么做吗?

I have a key value pair which i want to send to server.
e.g:
var obj = {'item1': true, 'item2': false, ........};

I want to send this information to server by ajax call.
But at server side i am unable to get individual value. At server side i am getting "object" as string.
I am using jQuery for making an ajax call.

Can anyone please give any idea how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

差↓一点笑了 2024-09-20 16:49:24

我得到了我的问题的答案。这可以通过使用以下方法来完成:

var obj = {'item1': true, 'item2': false, ........};
$.post("test.php", {data: JSON.stringify(obj)});

I got answer to my question. This can be done by using:

var obj = {'item1': true, 'item2': false, ........};
$.post("test.php", {data: JSON.stringify(obj)});
平定天下 2024-09-20 16:49:24
var a = $.JSON.encode(obj);
$.post("test.php", {data:a});

使用 JSON 解码器 转换 $_POST 中的字符串[“data”] 到服务器端的关联数组。

<?php
  $json = $_POST["data"]
  var_dump(json_decode($json));
?>
var a = $.JSON.encode(obj);
$.post("test.php", {data:a});

Use a JSON decoder to convert the string in $_POST["data"] to an associate array at the server side.

<?php
  $json = $_POST["data"]
  var_dump(json_decode($json));
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文