【问题标题】:Don't read the JSON Object in node.js不要读取 node.js 中的 JSON 对象
【发布时间】: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。为什么要转换成字符串?您正在尝试读取 stringrequestInfo 属性,而不是对象。删除JSON.stringify,工作完成。

标签: javascript jquery json ajax node.js


【解决方案1】:

@jeremy-thille 已经回答了这个问题,但要更清楚,只需更改

dataType: "text"

dataType: "json"

并删除 JSON.stringify 调用....

【讨论】:

    猜你喜欢
    • 2017-02-24
    • 2018-10-08
    • 2016-01-26
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多