【发布时间】:2017-05-05 10:33:36
【问题描述】:
服务器端代码为:
app.post('/', function(req, res, next) {
console.log(req.body);
res.json({ "a": "128", "b": "7" });
});
服务器端接收正确,打印为:
{ '{"type":"type","age":32,"married":false}': '' }
客户端为:
$.ajax({
type: "POST",
url: self.serverURI,
data: JSON.stringify(self.gen_data()),
dataType: 'json',
success: function (result)
{
//alert(result);
alert(JSON.parse(result));
},
error: function (xhr, ajaxOptions, thrownError)
{
console.log(xhr);
}
});
self.gen_data=function()
{ //create request data
var obj = new Object();
obj.type = "type";
obj.age = 32;
obj.married = false;
return obj;
}
得到服务器响应后
alert(result);
显示
[object object]
所以,我尝试解析
alert(JSON.parse(result));
并且没有弹出警报,所以假设它无法解析它。 我想交换里面包含数组的 JSon 数据,但是这些简单的测试不起作用。
有什么问题?谁能给我解释一下?
谢谢!
【问题讨论】:
-
似乎您收到的是解析结果而不是字符串。
-
试试
alert(result.a);或alert(JSON.stringify(result)) -
你不能在警告框中显示javascript对象,最好使用console.log
-
尝试 JSON.stringify 而不是解析
-
{ '{"type":"type","age":32,"married":false}': '' }真的很奇怪。这是一个对象,它有一个键,它是一个 json 对象的字符串表示形式......而且这个键的值是空的。