在OPTIONS请求后收到200 OK后,Firefox和Internet Explorer不发送POST请求 - 在Chrome中正常工作

Boa*_*rdy 8 api ajax jquery http

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

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

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

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

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

以下是Chrome和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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

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);
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

我正在使用Jquery来执行ajax帖子.

任何人都可以看到为什么在这种情况下Firefox将不会在200 OK之后发送实际请求,请求和响应看起来是相同的,这在Google Chrome中完美运行.

Boa*_*rdy 3

感谢@Manoj Purohit 评论,我已经想通了再次检查控制台。我这样做了,发现有一些警告已被过滤,我必须添加以下标头才能使其在 Firefox 和 Internet 中工作 - 尽管它在 Chrome 中被接受,但很奇怪。

this->addHeader("Access-Control-Allow-Headers", "authorisation-token, device_id, session_id");
Run Code Online (Sandbox Code Playgroud)