[英]Post request to page, not executing success function
我的javascript文件中包含以下代碼:
$.ajax({type: 'POST',
url: '/',
data: {test: 'This is some random data.'},
dataType: 'json',
success: function(){
console.log('success');
},
error: function(){
console.log('err');
}
});
在節點中,我有以下內容:
app.post('/', function (req, res){
console.log(req.body.test);
});
我的命令控制台正確記錄了json對象,但是我的網頁未執行on success函數。 我不明白為什么如果正確地傳遞和接收數據,我的ajax請求中會調用錯誤函數?
在節點js端,您必須對請求進行響應,否則客戶端不知道請求如何進行,因此無法觸發成功或錯誤回調:
app.post('/', function (req, res){
console.log(req.body.test);
res.end();
// or to send json back
// res.json({hello: 'world'});
// to go in error callback just send a request with a status code > 400
// res.status(400).end('bad request');
});
您只能發送一個回復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.