2017-07-12 74 views
1

我想使用請求模塊發佈數據。這裏是代碼,錯誤:無效的JSON使用Node.js請求模塊

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) 
+1

它適用於我也是這樣。節點版本:7.10,如果有幫助。 – Lazyexpert

+1

共享錯誤堆棧跟蹤。 –

+1

@MukeshSharma堆棧跟蹤已更新 – iJade

回答

0

請檢查JSON結構

JSON密鑰必須爲String

更新JSON結構

var requestInfo = {"tags": [{ 

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