HTTP preflight (OPTIONS) request fails in IE only(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
这篇关于HTTP 预检 (OPTIONS) 请求仅在 IE 中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HTTP 预检 (OPTIONS) 请求仅在 IE 中失败
基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01