2017-02-01 18 views
1

我得到body-parser內以下錯誤失敗,當我嘗試使用fetch取POST上後JSON體sails.js

1] error: Unable to parse HTTP body- error occurred :: 
'SyntaxError: `Unexpected token -\n at parse (/project/node_modules/body-parser/lib/types/json.js:83:15)\n  
    at /project/node_modules/body-parser/lib/read.js:116:18\n at invokeCallback (/project/node_modules/raw-body/index.js:262:16)\n at done (/project/node_modules/raw-body/index.js:251:7)\n at IncomingMessage.onEnd (/project/node_modules/raw-body/index.js:307:7)\n at emitNone (events.js:86:13)\n at IncomingMessage.emit (events.js:185:7)\n at endReadableNT (_stream_readable.js:973:12)\n at _combinedTickCallback (internal/process/next_tick.js:74:11)\n at process._tickDomainCallback (internal/process/next_tick.js:122:9)`' 

我調試的body-parser要發佈一個簡單的json身體在我的控制器我之所以發現爲什麼解析失敗是因爲收到一個字符串包裝

------WebKitFormBoundary. 

if (strict) { 
    console.log("body--->", body); 
    var first = firstchar(body) 

    if (first !== '{' && first !== '[') { 
    debug('strict violation') 
    throw new SyntaxError('Unexpected token ' + first) 
    } 
} 

body---> ------WebKitFormBoundaryE7nkf6ervMA9VlRo 
[1] Content-Disposition: form-data; name="json" 
[1] 
[1] {"hola":1} 
[1] ------WebKitFormBoundaryE7nkf6ervMA9VlRo-- 

我不知道如何擺脫這一點。

請求很簡單

var data = new FormData(); 
data.append("json", JSON.stringify({"hi": 1})); 
const options = { 
    credentials: 'include', 
    body : body 
}; 

var request = new Request('/api/settings', { 
    method: 'POST', 
    redirect: 'follow', 
    headers: new Headers({ 
     'Content-Type': 'application/json' 
    }) 
}); 

fetch(request, options).then(function(response) { 
    ... 
}) 

定製控制是有點無關緊要,因爲我的請求不會到達express路線由帆包裹。

'POST /settings': { 
    controller: 'SettingsController', 
    action: 'save' 
}, 

請求詳細信息。

Request URL:https://localhost:3005/settings 
Request Method:POST 
Status Code:400 Bad Request 
Remote Address:[::1]:3005 
Response Headers 


HTTP/1.1 400 Bad Request 
Content-Type: text/html; charset=utf-8 
Content-Length: 879 
ETag: W/"36f-yM7k6L+nVm6aoKcn9HH5aQ" 
Date: Wed, 01 Feb 2017 16:00:15 GMT 
Connection: keep-alive 
Request Headers 
view parsed 

POST /settings HTTP/1.1 
Host: localhost:3005 
Connection: keep-alive 
Content-Length: 145 
Pragma: no-cache 
Cache-Control: no-cache 
accept: application/json, application/xml, text/plain, text/html, *.* 
Origin: https://localhost:3005 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36 
content-type: application/json 
Referer: https://localhost:3005/ 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US,en;q=0.8 
Cookie: sails.sid=s%3AjzHwjq3AK12uG5AMQTQRZLoQ-7WaWeWN.MP3Joor2lBWEMbkuDqaXj7Y7%2Fdj1v8PJ9kGTJtTGkLY 
Request Payload 
------WebKitFormBoundary8A2lXn22y5Rxm12f 
Content-Disposition: form-data; name="json" 

{"hi":1} 
------WebKitFormBoundary8A2lXn22y5Rxm12f-- 

我嘗試了幾個樣本,但似乎我在這裏忽略了一些東西。

+1

你嘗試打從郵遞員或任何其他請求發送程序的API終點? –

+0

嘗試一下薩普納上面寫的是什麼。用郵遞員訪問API並檢查。您的請求可能有問題。 您如何提出您的要求? –

+0

隨着郵差工作正常使用相同的配置,包含'content-type:application/json',我可以到達我的控制器。今天我在這裏找到了解決方案http://stackoverflow.com/questions/29775797/fetch-post-json-data,'fetch'我必須使用''Content-Type':'x-www-form-urlencoded'而不是另一個(我使用Chrome 56)來使它工作。所以我想知道爲什麼不用'content-type:application/json' –

回答

2

什麼原因創建FormData對象?

它可以簡化爲

fetch(new Request('/api/settings', { 
    method: 'POST', 
    redirect: 'follow', 
    headers: new Headers({ 
     'Content-Type': 'application/json' 
    }) 
    }), { 
    credentials: 'include', 
    body: JSON.stringify({ 
     "hi": 1 
    }) 
    }) 
    .then(r => console.log) 
    .catch(e => console.error) 

enter image description here

+1

謝謝,你說得對,如果我的身體是'json',那麼使用FormData是沒有意義的。謝謝。 –