在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>
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:XMLHttpRequest对http://webapi.com/api/controller/getactionmethod/需要跨域资源共享(CORS)。 SEC7119:XMLHttpRequest对http://webapi.com/api/controller/getactionmethod/需要CORS预检。
我真的卡住了,不知道还能尝试什么。有什么想法吗?