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