[英]Ajax POST to Node.js through Nginx not getting correct response
我正在嘗試通過Nginx將Ajax發布請求發送到node.js微服務。 要求如下:
$.post('http://localhost:80/api/signup', formData, function( data, status) {
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
}).done(function(data, status) {
alert( "second success" );
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
})
.fail(function(data, status) {
console.log('error');
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
})
.always(function(data, status) {
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
console.log('fiished');
});
該請求到達微服務。 但是響應如下(它始終是錯誤函數):
“數據”始終為:
{"readyState":0,"status":0,"statusText":"error"}
而且“狀態”始終為:
error
這不是成功或失敗的預期反應。 我怎樣才能解決這個問題?
此外,“ formData”參數在提交后作為查詢出現在url上。 我該如何阻止呢?
*更新
我也嘗試添加event.preventDefault();
但是現在它給了我以下幾點:
XMLHttpRequest cannot load http://localhost:3000/api/signup. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
{"readyState":0,"status":0,"statusText":"error"}
我也嘗試過:
$.ajax({
type: "POST",
url: "http://localhost:3000/api/signup",
data: JSON.stringify(formData),
success: function(dataString) {
console.log(JSON.stringify(dataString));
}, error: function(error) {
console.log(JSON.stringify(error));
}
});
但是,如上所述No 'Access-Control-Allow-Origin' header is present on the requested resource
相同的No 'Access-Control-Allow-Origin' header is present on the requested resource
。
更新在“所請求的資源上沒有'Access-Control-Allow-Origin'標頭”上,有一個有效且可能更為詳細的答案。 但是,我發現我在這里接受的答案更加直接和明確。
您是從瀏覽器還是本地主機執行ajax請求? 如果從瀏覽器執行它,則可能是CORS問題。 如果您希望能夠從瀏覽器執行該Ajax請求,則應修改服務器配置/代碼以允許跨域HTTP請求。
這看起來與您的問題類似。 Jquery Ajax Post中的CORS問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.