【发布时间】:2016-01-21 01:13:17
【问题描述】:
我正在尝试从 AngularJS 页面连接到 ASP.NET Web-API Web 服务,我得到以下信息
凭据标志为“true”,但“Access-Control-Allow-Credentials”标头为“”。必须为“true”才能允许凭据。 Origin 'http://localhost:221' 因此不允许访问。
var cors = new EnableCorsAttribute("http://localhost:221", "*","GET,PUT,POST,DELETE");
config.EnableCors(cors);
使用这个 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
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:221" />
</customHeaders>
</httpProtocol>
我收到此错误消息
“Access-Control-Allow-Origin”标头包含多个值 localhost:221、localhost:221,但只允许一个值。 Origin localhost:221 因此不允许访问。
这真的没有任何意义,因为我已经添加了一次它并没有找到它,但是我将它添加到 web.config 并得到一个错误,说它被添加了多次。我已经阅读了很多文章,似乎无法找到正确的答案。我正在使用谷歌浏览器。非常感谢您的帮助,因为我现在正在拔头发。
【问题讨论】:
-
检查这个答案:stackoverflow.com/questions/21664988/… 我不得不从 web.config 中删除设置并将它们纯粹留在代码中。可能是 IIS 配置问题
标签: c# asp.net angularjs asp.net-web-api