简体   繁体   English

Angular - > Web Api 2:“没有'Access-Control-Allow-Origin'标题”

[英]Angular -> Web Api 2: “No 'Access-Control-Allow-Origin' header”

Basically I'm trying to access my web api which is uploaded on Azure. 基本上我正在尝试访问上传到Azure上的web api。

I've added my CORS attribute to my WebApiConfig class: 我已将我的CORS属性添加到我的WebApiConfig类:

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

However whenever I send an Ajax request (using angularJS) which rightfully contains 但是每当我发送一个正确包含的Ajax请求(使用angularJS)时

Origin: http://localhost:51407

I get the error: 我收到错误:

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

What could be the issue? 可能是什么问题?

To fix this issue, you need to configure CORS-es. 要解决此问题,您需要配置CORS-es。 First go to "Web.config", find "system.webServer" tag, inside of it add next lines: 首先转到“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>

That is all. 就这些。

For .NET server can configure this in web.config as shown below 对于.NET服务器,可以在web.config中进行配置,如下所示

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

I also work with webapi2 and AngluarJS client (different server) in Azure WebSites. 我还在Azure WebSites中使用webapi2和AngluarJS客户端(不同的服务器)。

please check my answer: https://stackoverflow.com/a/25758949/361100 请检查我的答案: https//stackoverflow.com/a/25758949/361100

If you cannot solve the problem, check other answers of the above link. 如果您无法解决问题,请查看以上链接的其他答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Web API的$ http角向给出XMLHttpRequest错误-请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头 - Angular $http to Web Api gives error of XMLHttpRequest -No 'Access-Control-Allow-Origin' header is present on the requested resource 从Angular JS调用到Web API错误:“ Access-Control-Allow-Origin”标头包含多个值,因此不允许访问Origin - Calling from Angular JS to web API error: The 'Access-Control-Allow-Origin' header contains multiple values, Origin is therefore not allowed access 角度:“ Access-Control-Allow-Origin”标头出现在请求的资源上 - Angular: 'Access-Control-Allow-Origin' header is present on the requested resource angular js nginx cors-没有&#39;Access-Control-Allow-Origin&#39;标头 - angular js nginx cors - No 'Access-Control-Allow-Origin' header 角$ http.post并且没有&#39;Access-Control-Allow-Origin&#39;标头 - Angular $http.post and No 'Access-Control-Allow-Origin' header 缺少CORS标头“ Access-Control-Allow-Origin”-PHP,角度 - CORS header 'Access-Control-Allow-Origin' missing - PHP, Angular Keycloak angular 不存在“Access-Control-Allow-Origin”标头 - Keycloak angular No 'Access-Control-Allow-Origin' header is present MVC web api:请求的资源上不存在“Access-Control-Allow-Origin”标头 - MVC web api: No 'Access-Control-Allow-Origin' header is present on the requested resource angularJS和Web API中的请求资源上没有&#39;Access-Control-Allow-Origin&#39;标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource in angularJS and Web API Web API CORS没有返回正确的Access-Control-Allow-Origin标头 - Web API CORS does not return correct Access-Control-Allow-Origin header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM