Http请求返回失败但实际上成功

时间:2015-08-04 14:19:48

标签: javascript c# asp.net angularjs asp.net-web-api

我正在开发一个网络API和一个与该API交互的网站。到目前为止,我完成了每个部分。我在我的网站上使用角度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>

1 个答案:

答案 0 :(得分:0)

将allow cross origin属性添加到api控制器

确保您已在webApiConfig文件中启用了CORS