jQuery $ .post和json_encode返回带有引号的字符串

问题描述

json_encode()返回一个字符串。从json_encode()文档中:

Returns a string containing the JSON representation of value.

你需要调用JSON.parse()data,这将解析JSON字符串,并把它变成一个对象:

$.post("getSale.PHP", function(data) {
    data = JSON.parse(data);
    console.log('data = '+data); // is showing the data with double quotes
}, 'json');

但是,由于在调用中将字符串连接data =到,将记录的是,它将返回对象的字符串表示形式。因此,您将要登录一个单独的呼叫。像这样:data``console.log()``data.toString()``[object Object]``data``console.log()

$.post("getSale.PHP", function(data) {
    data = JSON.parse(data);
    console.log('data = '); // is showing the data with double quotes
    console.log(data);
}, 'json');

解决方法

我正在使用jQuery的$ .post调用,它返回的字符串带有引号。引号由json_encode行添加。如何停止添加引号?我的$
.post通话中是否缺少某些内容?

$.post("getSale.php",function(data) {
    console.log('data = '+data); // is showing the data with double quotes
},'json');