【发布时间】:2014-12-05 21:47:37
【问题描述】:
在 IE8 和 9 中,我在执行 CORS webapi 调用时收到以下 JavaScript 错误:
Error: Access is denied.
{
[functions]: ,
description: "Access is denied.",
message: "Access is denied.",
name: "Error",
number: -2147024891
}
我按照这里http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api的描述设置了我的WebApi
所以 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 调用适用于 chrome 和 IE 10。IE10 打印:
SEC7118:http://webapi.com/api/controller/getactionmethod/ 的 XMLHttpRequest 需要跨域资源共享 (CORS)。 SEC7119:http://webapi.com/api/controller/getactionmethod/ 的 XMLHttpRequest 需要 CORS 预检。
我真的被困住了,不知道我还能尝试什么。有什么想法吗?
【问题讨论】:
-
我使用的是 angularjs 而不是 jQuery。
-
IE8 和 IE9 不支持标准的 CORS XHR。有关更多信息,请参阅我对这个问题的回答stackoverflow.com/questions/25141650/…
-
MrCode 是正确的。您需要对旧版浏览器使用 JSONP。 caniuse.com/#search=CORS
-
除了做 JSONP 请求来解决 IE8/9 中的 CORS 问题,肯定还有其他解决方案
标签: javascript c# angularjs asp.net-web-api