问题:
使用jQuery POST提交数据到PHP文件, PHP返回的json_encode后的数组数据, 但jQuery接收到的数据不能解析为JSON对象,而是字符串{"code":-1,"msg":"123","data":[]}
分析:
jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据。
jQuery $.get()方法
$.get() 方法通过 HTTP GET 请求从服务器上请求数据。
语法:$.get(URL,callback);
详细语法:
$(selector).get(url, data, success(response,status,xhr), dataType)
jQuery $.post() 方法
.post(URL,data,callback);
详细语法:
 jQuery.post(url,data,success(data, textStatus, jqXHR),dataType)
查看$.post()详细的语法:
jQuery.post(url,data,success(data, textStatus, jqXHR),dataType)
你会发现,最后边有个参数 dataType,这个就是问题所在。
这个dataType是可选参数,它规定预期的服务器响应的数据类型。默认执行智能判断(xml、json、script 或 html)。
>详细说明
该函数是简写的 Ajax 函数,等价于:
$.ajax({
 type: 'POST',
 url: url,
 data: data,
 success: success,
 dataType: dataType
});
>解决:
$.post() 加上最后边的一个可选参数 dataType 为“json”类型
示例:获得 test.php 页面返回的 json 格式的内容:
$.post("test.php", { "func": "getNameAndTime" },
 function(data){
 alert(data.name); // John
 console.log(data.time); // 2pm
 }, "json");
示例:获得 test.php 页面的内容,并存储为 XMLHttpResponse 对象,并通过 process() 这个 JavaScript 函数进行处理:
$.post("test.php", { name: "John", time: "2pm" },
 function(data){
 process(data);
 }, "xml");