問(wèn)題描述
我嘗試向我的 REST API 發(fā)出 POST 請(qǐng)求.這是代碼片段(使用 AngularJS):
$http({方法:'POST',網(wǎng)址:網(wǎng)址,數(shù)據(jù):reqBody,標(biāo)題:{內(nèi)容類型":應(yīng)用程序/json"}}).then(函數(shù)(響應(yīng)){...}).catch(函數(shù) (錯(cuò)誤) {...});
根據(jù)
問(wèn)題是,HTTP 選項(xiàng)響應(yīng)包含瀏覽器處理實(shí)際 HTTP 請(qǐng)求所需的一切.
我找到了所有這些混亂的原因.
API 服務(wù)和網(wǎng)站位于同一個(gè)域中,但在不同的端口上.具體來(lái)說(shuō),API 服務(wù)位于:
<塊引用>myDomain.com/apiService
網(wǎng)站位于:
<塊引用>myDomain.com:44443/webSite
因此,當(dāng)網(wǎng)絡(luò)瀏覽器從以下位置初始化調(diào)用時(shí):
myDomain.com:44443/webSite/page1
到:
myDomain.com/apiService/service1
由于 CORS,Internet Explorer 阻止了調(diào)用.出于某種原因,Chrome 在這方面沒有那么嚴(yán)格,因?yàn)樗晒Φ卣{(diào)用了 API.
為了使其在 Internet Explorer 中運(yùn)行,我將網(wǎng)站移至與 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
這篇關(guān)于HTTP 預(yù)檢 (OPTIONS) 請(qǐng)求僅在 IE 中失敗的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!