【问题标题】:Angular -> Web Api 2: "No 'Access-Control-Allow-Origin' header"Angular -> Web Api 2:“没有 'Access-Control-Allow-Origin' 标头”
【发布时间】:2014-12-15 22:15:01
【问题描述】:

基本上我正在尝试访问我在 Azure 上上传的 web api。

我已将我的 CORS 属性添加到我的 WebApiConfig 类中:

public static void Register(HttpConfiguration config)
{
    var cors = new EnableCorsAttribute("http://localhost:51407", "*", "*");
    config.EnableCors(cors);

但是,每当我发送正确包含的 Ajax 请求(使用 angularJS)时

Origin: http://localhost:51407

我得到错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:51407' is therefore not allowed access.

可能是什么问题?

【问题讨论】:

    标签: angularjs api azure asp.net-web-api cors


    【解决方案1】:

    要解决此问题,您需要配置 CORS-es。 首先进入“Web.config”,找到“system.webServer”标签,在里面添加下一行:

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
    

    就是这样。

    【讨论】:

      【解决方案2】:

      对于.NET服务器可以在web.config中进行如下配置

       <system.webServer>
         <httpProtocol>
           <customHeaders>
             <add name="Access-Control-Allow-Origin" value="your_clientWebUrl" />
           </customHeaders>
         </httpProtocol>
       </system.webServer>
      

      【讨论】:

        【解决方案3】:

        我还在 Azure 网站中使用 webapi2 和 AngluarJS 客户端(不同的服务器)。

        请查看我的回答:https://stackoverflow.com/a/25758949/361100

        如果您不能解决问题,请查看上述链接的其他答案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-25
          • 2019-04-10
          • 1970-01-01
          • 2016-01-21
          • 1970-01-01
          • 2016-05-13
          • 2016-05-13
          • 2014-06-22
          相关资源
          最近更新 更多