Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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
Javascript json_decode php在有效json上返回null_Javascript_Php_Ajax_Json_Post - Fatal编程技术网

Javascript json_decode php在有效json上返回null

Javascript json_decode php在有效json上返回null,javascript,php,ajax,json,post,Javascript,Php,Ajax,Json,Post,我尝试通过javascript中的ajax post发送json对象,如下所示: $.ajax({ type: 'POST', url: 'testPost.php', data: {json: cond}, dataType: 'json', success: function(response)

我尝试通过javascript中的ajax post发送json对象,如下所示:

$.ajax({
                    type: 'POST',
                    url: 'testPost.php',
                    data: {json: cond},
                    dataType: 'json',
                    success: function(response) {
                        alert(response["json"]);
                    }
                });
cond表示类似以下内容的json对象(使用json.stringify转换):


[{“field”:“name”,“condition”:“将Ajax数据转换为

[{json:{“字段”:“名称”,“条件”:”
您需要在echo之前在PHP中添加这一行

然后


print\u r(json\u encode(…)
不返回有效的json。只需使用
print
echo
而不是
print\u r()
。因此,您将永远不会输入
success
函数,尽管您当然可以在控制台中检查响应。替换
print\r(json\u encode($reply))
使用
echo json_encode($reply);
只要$reply是一个数组,它就可以工作。不好,问题仍然存在。你能检查
$\u POST[“json”]
的内容吗?试着单独运行PHP脚本并检查错误。可能引发通知(或警告)的东西事实上,$reply没有声明为数组,因此您应该首先声明它,然后将其用作key=>value。另外,检查是否也设置了$\u POST['json']。即使进行了此更改,json解码仍然会将null更改的'json'返回为“json”,请重试。这不需要,他使用的是
数据类型:“json”
…from“dataType是您期望从服务器返回的数据类型。”您应该知道并非所有服务器的环境都是相同的。在某些情况下,它是必需的。您可以在stackoverflow上搜索此问题,这是一个非常常见的问题。我修复了此问题,但有点傻。在php的顶部,我做了以下操作:[php]$return=$\u POST[“json”];$encode_json=json_encode($return);$decoded_json=json_decode($encode_json,true);
    <?php
       $return=$_POST["json"];
       $decoded_json=json_decode($return);
       $reply["json"]=$decoded_json;
       print_r ( json_encode($reply));
?>
$.ajax({
    type: 'POST',
    url: 'testPost.php',
    data: {"json": cond},
    dataType: 'json',
    success: function(response) {
        alert(response["json"]);
    }
});
<?php 
    if(count($_POST) > 0){
        print_r($_POST);
        exit;
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Document</title>
    <script src="jquery.js"></script>
    <script>
    $(function(){
        var cond = [{"field":"name","condition":"<","value":"John"}];
        $.ajax({
            type: 'POST',
            url: 'a.php',
            data: {"json" : cond},
            dataType: 'text',
            complete: function(response) {
                $("body").html(response.responseText);
            }
        });
    })
    </script>
</head>
<body>
</body>
</html>
header('Content-Type: application/json');
$.ajax({
    type: 'POST',
    url: 'testPost.php',
    data: {json: cond},
    dataType: 'json',
    success: function(response) {
        alert(response.field);
        alert(response.condition);
        alert(response.value);
    }
});