HTTP预检(OPTIONS)请求仅在IE中失败 [英] HTTP preflight (OPTIONS) request fails in IE only

查看:434
本文介绍了HTTP预检(OPTIONS)请求仅在IE中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

根据这篇文章,由于HTTP标头

According to this article, because of the HTTP header

内容类型":"application/json"

'content-type': 'application/json'

浏览器得出的结论是,它必须发出一个非简单" HTTP请求,该请求需要与服务器握手(HTTP选项请求将在实际HTTP请求之前发送).

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像超级按钮一样处理请求,但 IE(在我的情况下为11)失败,并显示以下消息:

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

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

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.

API服务和网站位于相同的域,但位于不同的端口.具体来说,API服务位于:

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

myDomain.com/apiService

并且网站位于:

myDomain.com:44443/webSite

myDomain.com:44443/webSite

因此,当网络浏览器正在初始化来自以下位置的呼叫时:

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

myDomain.com:44443/webSite/page1

收件人:

myDomain.com/apiService/service1

由于CORS,Internet Explorer阻止了该呼叫.出于某种原因,Chrome在此方面的要求较宽松,因为它成功调用了该API.

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.

要使其在Internet Explorer中工作,我将网站移至了与API相同的端口:

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

myDomain.com/apiService

myDomain.com/apiService

myDomain.com/webSite

myDomain.com/webSite

这篇关于HTTP预检(OPTIONS)请求仅在IE中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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