[英]AJAX POST request to server not working
我正在使用node.js構建應用程序,並且具有以下POST請求:
var api = express.Router();
$.ajax({
type: "POST",
url: "/api/login/",
data: context,
success: function(data, status) {
console.log('succeeded!!');
},
error: function(data, status, res) {
console.log('error :(');
console.log(res);
}
});
服務器端API:
api.post('/api/login/', function(req, res) {
console.log('login api hit'); //does not console log
});
問題是服務器未收到POST請求。 我在服務器端通過控制台日志記錄文本進行了檢查,但是它不返回任何內容...
有人知道會發生什么嗎?
提前致謝!
也許問題是ajax調用。 嘗試通過執行以下腳本來測試服務器:
var http = require('http');
var options = {
host: '127.0.0.1',
port: 8080,
// if it's not localhost, put 'hostname' instead host and port
//hostname: 'some_url.net',
path: '/api/login/',
method: 'POST',
dataType: 'json'
};
function sendRequest( data ) {
var req = http.request( options, function( res ) {
console.log( 'STATUS: ' + res.statusCode );
//console.log( 'HEADERS: ' + JSON.stringify(res.headers) );
res.setEncoding( 'utf8' );
res.on( 'data', function( chunk ) {
console.log( 'BODY: ' + chunk );
});
req.on('error', function( e ) {
console.log( 'problem with request: ' + e.message );
});
});
// write data to request body
req.write( JSON.stringify(data) );
req.end();
}
sendRequest( {"some":"data"} );
希望它可以幫助您找出問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.