【问题标题】:Error: Invalid json using Node.js request module错误:使用 Node.js 请求模块的 json 无效
【发布时间】:2017-07-12 11:45:14
【问题描述】:

我正在尝试使用请求模块发布数据。这是代码,

var requestInfo = {
        tags: [
            {
                "name": 'UIW_IWIWU',
                "filters": {
                    "attributes": {
                        "VesselId": '2982'
                    }
                },
                "order": "asc"   
            }
        ],
        start: "15mi-ago",
        end: "30ms-ago"
    };


    request.post({
            uri:'http://localhost:3000/data-api',
            json: 'true',
            body: requestInfo
        },function (error, response, body) {
            res.send(response);         
        }
    );

以上代码在终端抛出JSON无效错误。

Error: Invalid json

这是堆栈跟踪:

Error: invalid json
    at parse (D:\Working\branches\Smartship-SNG_Demo\node_modules\body-parser\li
b\types\json.js:83:15)
    at D:\Working\branches\Smartship-SNG_Demo\node_modules\body-parser\lib\read.
js:108:18
    at invokeCallback (D:\Working\branches\Smartship-SNG_Demo\node_modules\raw-b
ody\index.js:262:16)
    at done (D:\Working\branches\Smartship-SNG_Demo\node_modules\raw-body\index.
js:251:7)
    at IncomingMessage.onEnd (D:\Working\branches\Smartship-SNG_Demo\node_module
s\raw-body\index.js:307:7)
    at emitNone (events.js:86:13)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

【问题讨论】:

  • 它也适用于我,也适用于终端。节点版本:7.10,如果有帮助的话。
  • 共享错误堆栈跟踪。
  • @MukeshSharma 堆栈跟踪已更新
  • 我认为,问题在于request.post 的回复。 http://localhost:3000/data-api 可能会返回非 json 数据,并将 application/json 设置为内容类型。

标签: json node.js request


【解决方案1】:

请检查 JSON 结构

JSON 键必须是字符串

更新 JSON 结构

var requestInfo = {"tags": [{

        "name": "UIW_IWIWU",
        "filters": {
            "attributes": {
                "VesselId": 2982
            }
        },
        "order": "asc"
    }],
    "start": "15mi-ago",
    "end": "30ms-ago"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多