我有一個Node.js的後端,我設置爲接收POST請求給定的URL取消。當我通過高級休息客戶端Chrome擴展程序發佈信息時,一切正常,帖子成功。我的後端代碼沒有錯誤。但是,當我從客戶端應用程序發佈帖子時,該呼叫將在「選項」請求中取消。我知道,經常有不正確的標題或跨來源問題做的,但是我已經在我的服務器設置以下規格:JQuery的AJAX POST請求大幹快上選項
節點應用:
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Accept');
Cient端代碼:
var sendObj = {};
sendObj.firstName = firstName;
sendObj.lastName = lastName;
sendObj.address = address;
sendObj.phone = phone;
sendObj.email = email;
sendObj.birthday = birthday;
sendObj.gender = gender;
sendObj.codingExperience = codingExperience;
sendObj.whyInterested = whyInterested;
sendObj.referralInformation = referralInformation;
if(referralInformation != "Other"){
sendObj.otherReferral = "N/A";
}
else{
sendObj.otherReferral = otherReferral;
}
var url = "http://url-to-server/postendpoint";
if(errors == false){
$.ajax({
type: "POST",
url: url,
contentType:"application/json",
dataType: "json",
beforeSend: function (xhr){
xhr.setRequestHeader("Content-Type", "application/json");
},
data: sendObj,
success: successOther()
});
}
else{
alert("Please fill in all the fields before submitting.");
}
我已經通過我的請求進行搜索,看是否有任何從高級REST客戶端發送的是什麼不同,但我沒有找到任何東西。這是ARC的回覆信息。
請求報頭:
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,pt;q=0.6
Cookie: _ga=GA1.2.1462765942.1389934532
我喜歡任何幫助我能找出原因,這可能不是我的客戶端應用程序來工作。
這一定是它。我不知道jQuery添加了超出我所指定的額外頭部。謝謝! – John