在OPTIONS请求后收到200 OK后,Firefox和Internet Explorer不发送POST请求 - 在Chrome中正常工作 [英] Firefox and Internet Explorer not sending POST request after receiving 200 OK after OPTIONS request - Works fine in Chrome

查看:505
本文介绍了在OPTIONS请求后收到200 OK后,Firefox和Internet Explorer不发送POST请求 - 在Chrome中正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一项新服务,我在C ++应用程序中构建了一个REST API。 C ++应用程序侦听特定端口并接收HTTP / S流量,处理发送的内容,然后发回HTTP响应。

I am working on a new service where I have a REST API built inside a C++ application. The C++ applications listens on a particular port and receives HTTP/S traffic, processes what is sent, and then send an HTTP response back.

我的想法是,我将拥有不同的库,可以在C ++ API中发布REST API。我可以从任何地方和任何地方获得请求,因此它可能是另一个软件,例如通过CURL或来自浏览器的POST请求。

The idea is that I will have different libraries that will be able to post REST API within the C++ API. I can be getting a request from any where and anything, so it could be another bit of software, via CURL for example, or a POST request from a browser.

API正在运行,直到我正在使用一个与Javascript一起使用的库来通过AJAX帖子发送C ++ API请求。

The API is working until I was working on a library that would be used with Javascript to send the C++ API request via AJAX posts.

因为我正在从一个网站到另一个域做一个AJAX帖子,所以我不得不使用CORS。当我第一次开始设计时,我正在使用Chrome,我遇到了Chrome会发送HTTP OPTIONS请求的问题,我会回复403方法不允许,因为我当时不知道这一点。我查看了这个并找到了所需的内容,然后让它工作,以便Chrome发送OPTIONS请求,C ++应用程序将发送200 OK,然后Chrome将发送实际的AJAX POST。

Because I am doing an AJAX post from one website to another domain I am having to make use of CORS. When I first started designing this I was using Chrome and I hit a problem that Chrome would send an HTTP OPTIONS request and I would respond with a 403 Method Not Allowed as I didn't know about this at the time. I looked into this and found what was needed and then got it working so Chrome would send the OPTIONS request, the C++ app would send a 200 OK, and Chrome would then subsequently send the actual AJAX POST.

这完全适用于Chrome,但是,在Internet Explorer中进行测试时,Firefox浏览器会发送OPTIONS,而C ++应用程序会发回200 OK但是这两个浏览器都没有发送实际的POST请求。

This is fully working in Chrome, however, when testing in Internet Explorer, and Firefox the browser sends the OPTIONS, and the C++ app sends back a 200 OK but then neither of the browers send the actual POST request.

以下是Chrome和Firefox的请求标头和响应标头。

Below are the request headers and response headers from Chrome and Firefox.

Chrome请求标头

Request URL: http://192.168.1.96:500/initialise
Request Method: OPTIONS

Remote Address: 192.168.1.96:500
Referrer Policy: no-referrer-when-downgrade
Provisional headers are shown
Access-Control-Request-Headers: authorisation-token,device_id,session_id
Access-Control-Request-Method: POST
Origin: http://localhost
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36

Chrome回复标题

    Access-Control-Allow-Headers: * 
    Access-Control-Allow-Methods: POST, OPTIONS 
    Access-Control-Allow-Origin: * 
    Access-Control-Expose-Headers: session_id 
    Allow: POST,OPTIONS 
    Content-Length: 0 
    Content-Type: application/json
    Status Code: 200 OK

Firefox请求标题

Accept: text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language: en-GB,en;q=0.5
Access-Control-Request-Headers: authorisation-token,device_id,session_id
Access-Control-Request-Method: POST
Connection: keep-alive
Host: 192.168.1.96:500
Origin: http://localhost
Referer: http://localhost/_js/
User-Agent: Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/64.0
Request URL:http://192.168.1.96:500/initialise
Request method:OPTIONS
Remote address:192.168.1.96:500

Firefox响应标题

Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: session_id
Allow: POST,OPTIONS
Content-Length: 0
Content-Type: application/json
Status code:200

以下参考是我如何做ajax请求:

For reference below is how I am doing the ajax request:

var url = "http://192.168.1.96:500/";
        url += api_endpoint;

        $.ajax({
            type: "POST",
            url: url,
            async: true,
            headers: {
                "authorisation-token": app.api_key,
                "session_id": app.cookie,
                "device_id": app.device_id
            },
            data: postArray,
            crossDomain: true,
            success: function(object, status, xhr){
                if (api_endpoint === "initialise")
                {
                    app.cookie = xhr.getResponseHeader("session_id");
                    setCookie("session_id", app.cookie, true);
                }
                if (callbackResult !== null)
                {
                    callbackResult(object);
                }
            },
            error: function(xhr)
            {
                console.error("Status: " + xhr.status);
                console.error("Status Text:" + xhr.statusText);
                console.error("Response Text: " + xhr.responseText);
                if (callbackResult !== null)
                {
                    callbackResult(xhr);
                }
            }
        });

我正在使用Jquery执行ajax帖子。

I am using Jquery to perform the ajax post.

任何人都可以看到为什么在这种情况下Firefox不会在200 OK之后发送实际请求,请求和响应看起来是一样的并且这完全有效在谷歌浏览器中。

Can anyone see why in this case Firefox wouldn't be sending the actual request after the 200 OK, the request and the response looks to be the same and this works perfectly in Google Chrome.

推荐答案

我想通过@Manoj Purohit评论再次检查控制台。我这样做,发现有过滤的警告,我不得不添加以下标题,使其在Firefox和互联网上工作 - 虽然它在Chrome中已被接受,但很奇怪。

I've figured out thanks to @Manoj Purohit comment to check the console again. I did that and found there were warnings which had been filtered, I had to add the following header to make it work in Firefox and Internet - odd though that it was accepted in Chrome.

this->addHeader("Access-Control-Allow-Headers", "authorisation-token, device_id, session_id");

这篇关于在OPTIONS请求后收到200 OK后,Firefox和Internet Explorer不发送POST请求 - 在Chrome中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆