gpt4 book ai didi

php - 将 JSON 发送到 PHP

转载 作者:行者123 更新时间:2023-12-01 07:00:19 25 4
gpt4 key购买 nike

我尝试通过 jQueryJSON 发送到 PHP 页面,但它无法正常工作:

json_data = {};
json_data.my_list = new Array ();

$('#table_selected tr').each (function (i) {
json_data.my_list.push ({id:$(this).attr("id")});
});

$.post ("my_page.php", json_data, function (response) {
if (response) alert("success");
else alert("error");
});

<?php
// this is my_page.php
$json = json_decode (stripslashes ($_REQUEST['my_list']), true);
echo var_dump($json);

?>

这会向我的回调返回NULL,我哪里错了?

最佳答案

JSON 是 JavaScript 对象的字符串表示形式。您要发送的内容如下所示:

{my_list: [{id: 'foo'}, {id: 'bar'}, {id: 'baz'}]}

这不是 JSON。 这是 JSON:

'{"my_list": [{"id": "foo"}, {"id": "bar"}, {"id": "baz"}]}'

我建议使用json2.js (more info)。这可以通过使用 .serializeArray() 来促进。 .

关于php - 将 JSON 发送到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4480038/

25 4 0
文章推荐: python - 如何使用这些数据制作直方图?
文章推荐: java - 我是否应该始终重写 equals、hashcode 和 toString 方法?
文章推荐: python - 在 for 循环中自动更改索引
文章推荐: java - 使用 java 流从包含 List 的 List 检索 List