问题描述
我试图从AngularJS页面连接到ASP.NET Web-API Web服务,并且得到以下消息
I am trying to connect to a ASP.NET Web-API Web Service from an AngularJS page and I am getting the following
Credentials标志为'true',但'Access-Control-Allow-Credentials'标头为".允许使用凭据必须为"true".因此,不允许访问来源" http://localhost:221 ".
Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'http://localhost:221' is therefore not allowed access.
var cors = new EnableCorsAttribute("http://localhost:221", "*","GET,PUT,POST,DELETE");
config.EnableCors(cors);
使用此AngularJS
Using this AngularJS
$http({
method: 'GET',
url: 'http://localhost:1980/api/investors/62632',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
withCredentials: true
// withCredentials: true,
}).then(function onUserComplete(response) {
// this callback will be called asynchronously
// when the response is available
}, function onError(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
阅读许多文章后,我将其添加到web.config
After reading many articles I add this to the web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:221" />
</customHeaders>
</httpProtocol>
我收到此错误消息
"Access-Control-Allow-Origin"标头包含多个值localhost:221,localhost:221,但只允许一个.因此,不允许原始localhost:221进行访问.
The 'Access-Control-Allow-Origin' header contains multiple values localhost:221, localhost:221, but only one is allowed. Origin localhost:221 is therefore not allowed access.
这真的没有任何意义,因为我已经添加了一次并且找不到它,但是我将其添加到了web.config并收到一条错误消息,指出它已被多次添加.我已经阅读了许多文章,但似乎找不到正确的答案.我正在使用Google Chrome.非常感谢您的帮助,因为我现在正在拔头发.
Which really doesn't make any sense as I have added it once and it doesn't find it but I add it to web.config and get an error saying its been added multiple times. I have read many articles and can't seem to find the right answer. I am using Google Chrome. Would be very grateful for help as I am pulling my hair out right now.
推荐答案
使用WebApiConfig.cs的用户:
For whom, who uses WebApiConfig.cs:
config.EnableCors(new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true });
这篇关于凭据标志为"true",但"Access-Control-Allow-Credentials"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!