MVC Web API错误代码SCRIPT7002 [英] MVC Web API Error Code SCRIPT7002
问题描述
作为参考,我正在使用Windows 10和Visual Studio 2017。
For reference I'm using Windows 10 and Visual Studio 2017.
我有一个具有登录功能的MVC Web API和相应的Web应用程序。我只是尝试使用IE进行调试。每当我使用GET呼叫时,都会遇到以下错误:SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝。我已经配置并启用了CORS,所以我完全不知道为什么它不起作用。
I have an MVC web api and corresponding web application complete with login capabilities. I'm simply attempting to debug using IE. Whenever I use a GET call I'm met with the following error: SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied. I have CORS configured and enabled so I'm at a complete loss as to why this isn't working.
在我的WebApiConfig文件中,我有以下内容:
In my WebApiConfig file I have this:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableCors();
}
然后在Web.config文件中输入以下内容:
And then in the Web.config file I have this:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:53942" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
呼叫本身是使用以下代码进行的:
The call itself is being made using this code:
var returnValue = [];
console.log(localStorage.getItem('accessToken'));
jQuery.support.cors = true;
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", 'http://localhost:60690/api/ProfilePicture?Username=' + username, false);
xmlhttp.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xmlhttp.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('accessToken'));
xmlhttp.send();
if (xmlhttp.status == 200) {
returnValue = jQuery.parseJSON(xmlhttp.responseText);
}
}
return returnValue;
再次,我只是在尝试调试。部署项目后,它们将位于同一域中,因此CORS无关紧要。我缺少什么?
Again, I'm just trying to debug. When the projects are deployed they're going to be on the same domain so CORS is irrelevant. What am I missing?
推荐答案
好的,我终于明白了。这是我所做的:
Alright, I finally figured this out. Here's what I did:
- 在Web.config文件中,删除CustomHeader部分。摆脱一切。
-
找到了另一个StackOverflow问题:在OWIN上访问CORS并访问/ token会导致访问控制允许来源错误。具有讽刺意味的是,投票最多的答案是对我帮助最大的答案。我将以下行添加到GrantResourceOwnerCredentials类中:
- In the Web.config file remove the CustomHeader part. Get rid of it all.
Found this other StackOverflow question: CORS on OWIN and accessing /token causes 'Access-Control-Allow-Origin' error. Ironically the answer with the most downvotes was the one that helped me the most. I added in the following line into the GrantResourceOwnerCredentials class:
context.Response.Headers.Add( Access-Control-Allow-Origin,new [] { * });
context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
进入每个控制器并在顶部添加以下行:
Go into each controller and add the following line at the top:
[EnableCors(来源: http:// localhost:53942 ,标题: ,方法: ,SupportsCredentials = true)]
[EnableCors(origins: "http://localhost:53942", headers: "", methods: "", SupportsCredentials = true)]
一旦执行此操作,它就会开始工作。
Once I did this it started working.
这篇关于MVC Web API错误代码SCRIPT7002的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!