我正在使用 jQuery 的 $.post 调用,它返回一个带有引号的字符串。引号是由 json_encode 行添加的。如何阻止添加引号?我在 $.post 通话中遗漏了什么吗?
$.post("getSale.php", function(data) {
console.log('data = '+data); // is showing the data with double quotes
}, 'json');
json_encode()
返回一个字符串。来自json_encode()文档:
Returns a string containing the JSON representation of value.
你需要打电话JSON.parse()
on data
,它将解析 JSON 字符串并将其转换为对象:
$.post("getSale.php", function(data) {
data = JSON.parse(data);
console.log('data = '+data); // is showing the data with double quotes
}, 'json');
但是,由于您正在连接字符串data =
to 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');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)