[英]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.