【发布时间】:2015-08-04 14:19:48
【问题描述】:
我正在开发一个 Web API 和一个与该 API 交互的网站。到目前为止,我已经完成了每个部分。我在我的网站上使用 angular js,在另一端使用 asp.net webapi。当我尝试将两者连接在一起以使其全部正常工作时,信号发送并且api端像正常一样执行但是在网站端的控制台中给了我一个错误:
XMLHttpRequest cannot load http://localhost:1795/api/products/[ip
address]/en-us/[product]/[version]/[email address]. A wildcard '*' cannot
be used in the 'Access-Control-Allow-Origin' header when the credentials
flag is true. Origin 'http://localhost:50028' is therefore not allowed access.`
它似乎只是在循环中,因为如果我现在删除我为 Access-Control-Allow-Origin 提供的自定义标头,它仍然会触发我在 api 中的进程,该进程会正常执行,但我仍然会结束在另一端出现错误。
XMLHttpRequest cannot load http://localhost:1795/api/products/[ip
address]/en-us/[product]/[version]/[email address]. No 'Access-Control-
Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:50028' is therefore not allowed access.
如果我从 http 请求中删除凭据,则会收到 401 未经授权的错误,并且不会触发 api 进程。我在 Visual Studio 2013 中创建了这两个项目,并且都在 Chrome 中运行。
我是否会忽略网站方面的错误并保持原样?或者有没有其他方法可以在不出现这些控制台错误的情况下进行调用?
** 更新 ** 这是我用来添加自定义标头的配置
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
【问题讨论】:
-
你能发布你的CORS配置代码吗?
-
您使用 CORS 是否有特殊原因?如果网站和 API 在同一个域中,则不需要它。
-
@BenRobinson 这就是我的想法,但如果我出于某种原因不使用它,它就不会到达 api。
标签: javascript c# asp.net angularjs asp.net-web-api