将JSON发送到PHP
我试图通过将JSON发送到PHP,php,jquery,json,Php,Jquery,Json,我试图通过jQuery将JSON发送到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) {
jQuery
将JSON
发送到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);
?>
json_data={};
json_data.my_list=新数组();
$('#表u选定tr')。每个(功能(i){
json_data.my_list.push({id:$(this.attr(“id”)});
});
$.post(“my_page.php”、json_数据、函数(响应){
如果(响应)警报(“成功”);
else警报(“错误”);
});
这会将
NULL
返回到我的回调
,在那里我错了?json\u数据
是一个文本,其中包含一个数组,作为post的参数,并将在post请求中作为编码数组发送到服务器
在my_page.php
中,您可能需要查看$\u POST
数组
更新:嗯,我重读了你的问题,我不完全确定我写了什么。我所说的适用于GET请求,我相信也适用于post请求。
json_data
是一个文本,其中包含一个数组,作为post的参数,并将作为post请求中的编码数组发送到服务器
在my_page.php
中,您可能需要查看$\u POST
数组
更新:嗯,我重读了你的问题,我不完全确定我写了什么。我所说的适用于GET请求,我相信也适用于post请求。在var_dump之前不需要echo在var_dump之前不需要echoJSON是JavaScript对象的字符串表示。您发送的内容如下所示:
{my_list: [{id: 'foo'}, {id: 'bar'}, {id: 'baz'}]}
这不是JSON。这是JSON:
我建议使用()。这可以通过使用来实现。JSON是JavaScript对象的字符串表示形式。您发送的内容如下所示:
{my_list: [{id: 'foo'}, {id: 'bar'}, {id: 'baz'}]}
这不是JSON。这是JSON:
我建议使用()。这可以通过使用