javascript - 带有 AngularJS CORS 的 IE9、IE8 返回 “Access is denied” - ASP.NET WebApi

标签 javascript c# angularjs asp.net-web-api

在 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 应用程序:

  <!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>

应用程序.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 预检。

我真的卡住了,不知道我还能尝试什么。有什么想法吗?

最佳答案

在执行 CORS 请求时,我遇到了与 IE8/9(Django 后端而不是 ASP.NET)相同的问题。

有几种方法可以解决这个问题。对我来说最简单和最快的解决方案是使用 jpillora 中的 polyfill .使用此 polyfill,正常的 CORS XMLHttpRequests 将在 IE8/9 上换成 XDR。

包括XHook并在您的网站上添加以下 before-hook:

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
});

还有其他几种解决方案:

关于javascript - 带有 AngularJS CORS 的 IE9、IE8 返回 “Access is denied” - ASP.NET WebApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26304245/

相关文章:

javascript - 将 Canvas 导出到 SVG 文件

C# - 在任何计算机上播放程序中的音频?

angularjs - 使用 ng-token-auth 检索身份验证状态和当前用户

javascript - 使用 Angular ui Bootstrap 显示选项卡 - 选项卡集

javascript - 使用 jQuery 允许打开 Accordion 部分的最大数量

javascript - CORS:凭据模式为 'include'

javascript - Angular UI 日期选择器获取错误的日期

c# - Azure Web 应用程序上出现 BadImageFormatException 错误

c# - 我不太明白 MySql 连接是什么样的

php - 如何避免在客户端和服务器之间重复业务逻辑?