2014-10-20 32 views
6

基本上我試圖訪問我上傳到Azure上的web api。Angular - > Web Api 2:「No」Access-Control-Allow-Origin'header「

我已經添加了我的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. 

可能是什麼問題?

回答

1

對於.NET服務器可以在web.config中配置此如下所示

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