0
我正在使用另一個項目中的模式將JSON數據發送到節點。在另一個項目中,它在這個項目中不起作用。 req對象是valide,但是body是空的。爲什麼?請求主體爲什麼是空的?
客戶端:
json = { "auth_user_pkref": 2 }
json.test_id = "ANCA"
json.questions_count = 3
json.right_answers = 2
json.wrong_answers = 1
json.seconds_spent = 180
console.log('/api/answer/add', json);
$.ajax({
url: "/api/answer/add",
type: "post",
data: JSON.stringify(json),
contentType: "application/json",
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("ERROR, DB error");
}
});
服務器端:
router.post('/api/answer/add', function (req, res) {
console.log('/api/answer/add: ', req.body)
服務器日誌:
/api/answer/add: undefined
JSON.stringify(json)返回字符串,但contentType:「application/json」。嘗試刪除'contentType:「application/json」' –
在'app.use'中添加'bodyParser.json()'?像'app.use(bodyParser.json());'? – zangw
也嘗試刪除'JSON.stringify('從'data:JSON.stringify(json),' – zangw