微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。