[英]XDomainRequest (CORS) for XML causing “Access is denied” error in IE8 / IE9
[英]IE9,IE8 with AngularJS CORS returns “Access is denied” - ASP.NET WebApi
在IE8和9中,當我正在進行CORS webapi調用時,我收到以下JavaScript錯誤:
Error: Access is denied.
{
[functions]: ,
description: "Access is denied.",
message: "Access is denied.",
name: "Error",
number: -2147024891
}
我設置了像這里描述的WebApi http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api
所以WebApi包含:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
[...]
我的測試AngularJS App:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ng="http://angularjs.org" ng-app="app">
<head>
<title>test</title>
<script src="Scripts/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="testController as vm">
{{vm.test}}
{{vm.data}}
</div>
</body>
</html>
app.js:
var app = angular.module('app');
app.controller('testController', function ($http) {
var vm;
vm = this;
vm.test = "bla non no ";
vm.data = null;
$http.defaults.headers.common['Authorization'] = 'a token'
return $http({
method: 'GET',
data: null,
url: 'http://webapi.com/api/controller/getactionmethod/',
}, function (data) {
console.log("bla");
}).success(function (data, status, headers, config) {
console.log("bla a");
vm.data;
});
});
上面的代碼/ webapi調用適用於chrome和IE 10. IE10打印:
SEC7118:用於http://webapi.com/api/controller/getactionmethod/的 XMLHttpRequest需要跨源資源共享(CORS)。 SEC7119:用於http://webapi.com/api/controller/getactionmethod/的 XMLHttpRequest需要CORS預檢。
我真的被卡住了,不知道我能嘗試什么。 有任何想法嗎?
AngularJS v1.2.23不支持IE8或IE9的CORS請求。 但是IE8 / 9支持使用XDomainRequest對象限制的CORS。 另請參見http://msdn.microsoft.com/en-us/library/ie/cc288060(v=vs.85).aspx
我試着修改angularjs lib,就像這里描述的那樣http://samuellam.wordpress.com/2013/08/03/ie-89-cors-support-in-angular-js/
但我注意到我無法使用XDomainRequest請求發送自定義標頭。 所以我最終在同一台機器上使用相同的ips部署項目,這將適用於IE8和9,這實際上只是一種解決方法。
在執行CORS請求時,我遇到了與IE8 / 9(Django后端而不是ASP.NET)相同的問題。
有幾種方法可以解決這個問題。 對我來說最簡單,最快速的解決方案是使用jpillora的polyfill 。 使用此polyfill,將在IE8 / 9上交換正常的CORS XMLHttpRequests用於XDR。
包括XHook並在您的網站上添加以下掛鈎:
xhook.before(function(request, callback) {
//skip browsers that dont use XDR
if(!window.XDomainRequest)
return callback();
//skip requests that aren't cross domain
var url = request.url;
var loc = window.location;
var hostname = loc.hostname + (loc.port ? ":"+loc.port : "");
if(!/^https?:\/\/([^\?\/]+)/.test(url) || RegExp.$1 === hostname)
return callback();
//if not GET, force POST
var method = request.method;
if(method !== 'GET') method = 'POST';
//force same protocol
url = url.replace(/^https?:/,loc.protocol);
//request!
var xdr = new window.XDomainRequest();
xdr.timeout = request.timeout;
//proxy events
var proxy = function(e) {
xdr['on'+e] = function() {
request.xhr.dispatchEvent(e);
};
};
var events = ['progress','timeout','error'];
for(var i = 0; i < events.length; ++i )
proxy(events[i]);
//custom onload
xdr.onload = function() {
callback({
status: 200,
statusText: "OK",
headers: {
'Content-Type': xdr.contentType
},
text: xdr.responseText
})
};
xdr.open(method, url);
xdr.send(request.body);
return
});
還有其他幾種解決方案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.