HTTP 预检 (OPTIONS) 请求仅在 IE 中失败

我尝试向我的 REST API 发出 POST 请求.这是代码片段(使用 AngularJS):

 $http({方法:'POST',网址:网址,数据:reqBody,标题:{内容类型":应用程序/json"}}).then(函数(响应){...}).catch(函数 (错误) {...});

根据

问题是,HTTP 选项响应包含浏览器处理实际 HTTP 请求所需的一切.

解决方案

我找到了所有这些混乱的原因.

API 服务和网站位于同一个域中,但在不同的端口上.具体来说,API 服务位于:

<块引用>

myDomain.com/apiService

网站位于:

<块引用>

myDomain.com:44443/webSite

因此,当网络浏览器从以下位置初始化调用时:

myDomain.com:44443/webSite/page1

到:

myDomain.com/apiService/service1

由于 CORS,Internet Explorer 阻止了调用.出于某种原因,Chrome 在这方面没有那么严格,因为它成功地调用了 API.

为了使其在 Internet Explorer 中运行,我将网站移至与 API 相同的端口:

<块引用>

myDomain.com/apiService

myDomain.com/webSite

I trying to make a POST request to my REST API. Here is the code snippet (using AngularJS):

        $http({
            method: 'POST',
            url: url,
            data: reqBody,
            headers: {
                'content-type': 'application/json'
            }
        })
        .then(function (response) {...})
        .catch(function (error) {...});

According to this article, because of the HTTP header

'content-type': 'application/json'

browser concludes that it will have to make an "not-simple" HTTP request which requires handshake with a server (HTTP options request will be sent before actual HTTP request).

Chrome handles the request like a charm, but IE (11 in my case) fails with the following messages:

The thing is, HTTP options response contains everything the browser needs to proceed with the actual HTTP request.

解决方案

I found the reason for all that mess.

The API service and the website were located on the same domain, but on different ports. To be specific, the API service was located on:

myDomain.com/apiService

and the website was located on:

myDomain.com:44443/webSite

Thus, when the web browser was initializing the call from:

myDomain.com:44443/webSite/page1

to:

myDomain.com/apiService/service1

Internet Explorer was blocking the call because of the CORS. For some reason, Chrome was less strict in that matter, because it succeeded to make the call to the API.

To make it work in Internet Explorer, I moved the website to the same port as the API:

myDomain.com/apiService

myDomain.com/webSite

相关文章