3
爲什麼我無法收到在POST請求正文中發送的純文本?空req.body接收文本/純POST請求到node.js
從客戶端瀏覽器發出請求:
var xhr = new XMLHttpRequest();
xhr.open("POST", "/MyRoute/MySubRoute");
xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xhr.send("hello!");
使用快速與我的節點服務器:
app.post('/MyRoute/MySubRoute', function(req, res) {
console.log("Received:"+require('util').inspect(req.body,{depth:null});
res.send();
});
登錄到控制檯我得到:
Received:{}
我已經用text/plain
(沒有字符集)嘗試,結果相同。如果我將我的內容類型更改爲application/json
並傳遞簡單的JSON字符串,則可以正常工作。
快速'req.body'是什麼?也許'util.inspect()'函數不能以字符串的方式工作。 – gustavohenke
我還使用了沒有util.inspect()的console.log(req.body),並嘗試了JSON.stringify(req.body),結果肯定是空的,無論我怎麼看它。 ''typeof(req.body)''是''Object''。 – pancake
看看這裏,由TJ自己回答:http://stackoverflow.com/a/12497793/2083599 - 這不是最好的方式做它,但它比沒有好... – gustavohenke