【发布时间】:2016-06-10 13:14:50
【问题描述】:
我在获取 json 对象时遇到了一些问题...
我将一个 json 对象发送到 localhost(node.js 服务器)。
node.js 服务器收到 json 对象时,解析的 json 对象有 'undefined 类型。
因此,我检查了 json 对象,但它没有结构问题..
我该如何解决这个问题?
提前致谢!
$.ajax({
url: "http://127.0.0.1:62590/saveResource",
type: "post",
dataType: "text",
cache: false,
timeout: 30000,
data: JSON.stringify(jsonObject),
success: function (data) {
............
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + ' : ' + errorThrown);
}
});
================================================ ==============================
app.post('/saveResource', function (request, response) {
var resultObj = request.body;
var object = resultObj.requestInfo;
console.log(typeof(object)); -> 'undefined' type
});
================================================ ==============================
* node.js server reveceive the following object.
{
"requestInfo": {
"urlInformation": "data",
"methodInformation": "GET",
"bodyInformation": "data",
"headerInformation": []
}
}
【问题讨论】:
-
看在上帝的份上,人们不要再对 JSON 进行字符串化了。只需使用纯 JSON 数据即可。发送 JSON,接收 JSON,读取 JSON。为什么要转换成字符串?您正在尝试读取 string 的
requestInfo属性,而不是对象。删除JSON.stringify,工作完成。
标签: javascript jquery json ajax node.js