Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';getJSON无法工作_Php_Jquery_Ajax_Json - Fatal编程技术网

Php Can';getJSON无法工作

Php Can';getJSON无法工作,php,jquery,ajax,json,Php,Jquery,Ajax,Json,使用$.getJSON()发送和接收JSON对象时遇到问题 在index.html中,我发送一个get JSON请求来发送一个对象并检索数据 var newtable = { "query":"create", "num":"2" }; var crypt=JSON.stringify(newtable); $.getJSON('php/table_manager.php',crypt,function(data){alert(data);}); 在php/table_manager.php中

使用$.getJSON()发送和接收JSON对象时遇到问题

index.html中,我发送一个get JSON请求来发送一个对象并检索数据

var newtable = {
"query":"create",
"num":"2"
};
var crypt=JSON.stringify(newtable);
$.getJSON('php/table_manager.php',crypt,function(data){alert(data);});
在php/table_manager.php中,我编写了一个脚本来简单地返回JSON对象:

$data = json_decode($_POST);
$return = json_encode($data);
echo $return;

但是,
$return
为空。请帮助我解决这个问题。

当您向$.getJSON提交一个对象时,jQuery会将其解包成单独的变量,并发送到服务器。因此,在服务器上,您将有
$\u GET['query']=“create”
$\u GET['num']=“2”


您所需要做的就是json编码($GET)并将其发送回—无需首先解码。

函数
$。jQuery的ajax套件的getJSON
发送类型为“GET”的请求

php/table_manager.php,它是:

$data = json_decode($_POST);
$return = json_encode($data);
echo $return;

尝试通过
$\u POST
检索POST值,将其更改为
$\u GET
,您就可以开始了

您是否查看了
$\u POST
变量包含的内容?如果无法解码您提供给它的数据,则返回null,而json_encode可能只是协作存储null值。我打赌
$\u POST
不是你应该解码的东西-你可能需要对它做些什么来获得你想要的价值。里面可能有你需要合作的东西。嗯,我想这就是原因。那么如何在php中检索JSON对象呢?我不知道
$\u POST
是如何工作的,否则我会留下一个答案。查看脚本中的
$\u POST
变量并研究其文档。我上次使用PHP已经好几年了,所以我忘了。
$。getJSON
发送GET请求,所以您的答案应该是指
$\u GET
而不是
$\u POST