fetch POST fails on post json body with sails.js
当我尝试使用
1 2 3 4 5 6 7 8 9 10 11 12 13 | 1] error: Unable to parse HTTP body- error occurred :: 'SyntaxError: `Unexpected token - at parse (/project/node_modules/body-parser/lib/types/json.js:83:15) at /project/node_modules/body-parser/lib/read.js:116:18 at invokeCallback (/project/node_modules/raw-body/index.js:262:16) at done (/project/node_modules/raw-body/index.js:251:7) at IncomingMessage.onEnd (/project/node_modules/raw-body/index.js:307:7) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:973:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickDomainCallback (internal/process/next_tick.js:122:9)`' |
我调试了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ------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-- |
号
我不知道如何摆脱这种情况。
请求相当简单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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) { ... }) |
自定义控制器有点无关紧要,因为我的请求从未到达由帆包裹的
1 2 3 4 | 'POST /settings': { controller: 'SettingsController', action: 'save' }, |
。
请求详细信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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-- |
我试了几个样品,但似乎我忽略了一些东西。
创建FormData对象的原因是什么?
它可以简化为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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) |
。