2014-10-10 61 views
6

在IE8和9我收到的时候我做了CORS的WebAPI调用下面的JavaScript错误:IE9,IE8与AngularJS CORS返回“访问被拒绝” - ASP.NET的WebAPI

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应用:

<!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铬和IE 10. IE10打印作品:

SEC7118:XMLHttpRequest进行http://webapi.com/api/controller/getactionmethod/需要跨源资源共享(CORS)。 SEC7119:XMLHttpRequest的http://webapi.com/api/controller/getactionmethod/需要CORS预检。

我真的被卡住了,不知道我可以试试其他的东西。有任何想法吗?

+0

http://stackoverflow.com/questions/10232017/ie9-jquery-ajax-with-cors-returns-access-is-denied – epascarello 2014-10-10 16:48:53

+1

我使用的是angularjs而不是jQuery。 – Briefkasten 2014-10-10 16:50:04

+2

IE8和IE9不支持标准的CORS XHR。看到我对这个问题的答案更多信息http://stackoverflow.com/questions/25141650/xdomainrequest-vs-xmlhttprequest – MrCode 2014-10-10 16:57:35

回答

2

AngularJS v1.2.23不支持IE8或IE9的CORS请求。 但IE8/9支持CORS与XDomainRequest对象限制。又见http://msdn.microsoft.com/en-us/library/ie/cc288060(v=vs.85).aspx

我试图修改angularjs LIB喜欢这里descriped http://samuellam.wordpress.com/2013/08/03/ie-89-cors-support-in-angular-js/

但是我发现我不能发送与XDomainRequest请求自定义标题。所以我最终在具有相同ips的同一台机器上部署该项目,这将适用于IE8和9,这实际上只是一种解决方法。

http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

2

我做CORS请求时,曾与IE8/9(Django的后端,而不是ASP.NET)相同的问题。

有几种方法可以解决这个问题。对我来说最简单和最快的解决方案是使用jpillora的polyfill。使用这个polyfill标准CORS XMLHttpRequests将在IE8/9上换出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 
}); 

有几种其他的解决方案: