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

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

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_encode,如下所示:

  $json_encoded_string = json_encode(array("hello"=>'abc')); echo $json_encoded_string; die; 

但是alert(msg.hello)为我返回undefined 。 这里出了什么问题? 另外,在我的console.log中,我可以得到输出:

 {"hello":"abc"} 

在返回数据上使用parseJSON

 if (msg) { msg = $.parseJSON(msg); alert(msg.hello); } 

你必须将数据作为Content-Type“application / json”发送,否则它将无法工作。

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

 header('Content-type: application/json');