Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.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 json_encode未返回正确的json编码字符串_Php_Jquery_Ajax_Json - Fatal编程技术网

php json_encode未返回正确的json编码字符串

php json_encode未返回正确的json编码字符串,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我正在使用接受json响应的jquery ajax调用: var posturl = '/admin/getparamdetails/'; var data = "adnetworkId="+adnetworkId; $.ajax({ type: "POST", url: posturl, data : data, datatype: "json", success: function(msg){

我正在使用接受json响应的jquery ajax调用:

 var posturl = '/admin/getparamdetails/';
    var data = "adnetworkId="+adnetworkId;
    $.ajax({
        type: "POST",
        url: posturl,
        data : data,
        datatype: "json",
        success: function(msg){
            //$("#displayPramForm").html(msg);
            //alert('hello'+msg.length+' '+msg.hello.length);
            console.log(msg);
            if(msg!='')
            {
                alert(msg.hello);
            }
        },
        failure: function(msg){}
    });
在我的php后端函数中,我在一个简单数组上使用json_编码,如图所示:

 $json_encoded_string =  json_encode(array("hello"=>'abc'));
 echo $json_encoded_string;
 die;               
但是alert(msg.hello)为我返回未定义的。这里出了什么问题? 此外,在我的控制台.log中,我能够获得如下输出:

{"hello":"abc"}     

您必须以内容类型“application/json”发送数据,否则它将无法工作

只需在PHP文件中添加以下内容:

header('Content-type: application/json');
在返回数据上使用:

if (msg) {
  msg = $.parseJSON(msg);
  alert(msg.hello);
}
它应该是
header('Content-Type:application/json')