是的,我知道你在想什么——又一篇CORS问题,但这次我却被难住了。
首先,关于实际错误消息:
XMLHttpRequest无法加载http://localhost/Foo.API/token。当请求的凭证模式为'include'时,响应中的“Access-Control-Allow-Origin”头的值不能是通配符“*”。因此,来自“http://localhost:5000”的源不被允许访问。XMLHttpRequest发起的请求的凭证模式由withCredentials属性控制。
我不确定凭证模式为“include”指的是什么?
所以,当我在Postman中执行请求时,我没有遇到任何错误:
但是当我通过我的angularjs Web应用程序访问相同的请求时,我被这个错误难住了。下面是我的angularjs请求/响应。如你所见,响应是OK 200
,但我仍然收到CORS错误:
Fiddler请求和响应:
以下图像演示了从Web前端到API的请求和响应
因此,根据我在网上阅读的所有其他帖子,似乎我正在做正确的事情,这就是我无法理解这个错误的原因。最后,这里是我在angularjs中使用的代码(登录工厂):
API中的CORS实现-供参考:
使用的方法1:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
EnableCrossSiteRequests(config);
}
private static void EnableCrossSiteRequests(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*")
{
SupportsCredentials = true
};
config.EnableCors(cors);
}
}
使用了方法2:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
*
- 因此,服务器端正在错误地执行CORS - 哦,而Postman之所以有效是因为它不是跨源请求。 - Jaromanda Xaccess-control-allow-origin
响应必须是源(浏览器页面)主机,不能是*
。 - Jaromanda X