我正在使用jQuery的$ .post调用,它返回一个带引号的字符串.引号由json_encode行添加.如何阻止添加引号?我在$ .post电话中遗漏了什么?
$.post("getSale.php", function(data) {
console.log('data = '+data); // is showing the data with double quotes
}, 'json');
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 13
json_encode()返回一个字符串.从json_encode()文档:
Returns a string containing the JSON representation of value.
Run Code Online (Sandbox Code Playgroud)
你需要调用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');
Run Code Online (Sandbox Code Playgroud)
但是,因为你是串联串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');
Run Code Online (Sandbox Code Playgroud)