简体   繁体   English

jQuery ajax发布请求在选项上被取消

[英]JQuery ajax post request gets cancelled on options

I've got a Node.js backend which I set up to receive post requests to a given url. 我有一个Node.js后端,我将其设置为接收对给定URL的发布请求。 Everything works great, the post is successful, when I do the post via the Advanced Rest Client Chrome Extension. 当我通过Advanced Rest Client Chrome扩展程序进行发布时,一切正常,发布成功。 No errors in my backend code. 我的后端代码中没有错误。 When I do a post from my client application, however, the call gets cancelled on the Options request. 但是,当我从客户应用程序中发布帖子时,该呼叫在“选项”请求中被取消。 I know that often has to do with incorrect headers or cross-origin issues, however I've set up my server to the following specifications: 我知道通常与错误的标题或跨域问题有关,但是我已将服务器设置为以下规格:

Node App: 节点应用:

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-side Code: 客户端代码:

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.");
}

I've searched through my request to see if there's anything different from what is being sent in Advanced Rest Client, but I'm not finding anything. 我在请求中进行了搜索,以查看是否与Advanced Rest Client中发送的内容有所不同,但没有找到任何内容。 Here is the response info from ARC. 这是来自ARC的响应信息。

Request Headers: 请求标头:

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

I'd love any help I can get to figure out why this might not be working on my client app. 我很乐意提供任何帮助,以弄清为什么这可能无法在我的客户端应用程序上正常工作。

I'm not familiar with node but this might help: 我对节点不熟悉,但这可能会有所帮助:

https://gist.github.com/nilcolor/816580 https://gist.github.com/nilcolor/816580

So I still haven't found out what the issue is exactly. 因此,我仍然没有找出问题的确切原因。 However, I did find out that it has something to do with my JQuery call. 但是,我确实发现这与我的JQuery调用有关。 I decided to use an Angular.js $http.post() request instead for the post to the server and everything worked beautifully. 我决定使用Angular.js $http.post()请求代替将其发布到服务器,并且一切正常。 So I'm guessing I somehow set up my headers wrong in the JQuery post, because that's a common thing to cause the OPTIONS request to fail, but things seem to be working perfectly now! 因此,我猜测我在JQuery帖子中以某种方式设置了错误的标头,因为这是导致OPTIONS请求失败的常见现象,但现在看来一切正常! If anybody would have any idea as to exactly what I'm doing wrong that would be fantastic to find out! 如果有人对我在做什么错有任何想法,那真是太好了!

Try adding X-Requested-With as an allowed header. 尝试添加X-Requested-With作为允许的标头。 jQuery adds this header when it sends the AJAX request. jQuery在发送AJAX请求时添加了此标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM