如何在web api 2中启用跨源请求? [英] How to enable cross-origin request in web api 2?

查看:139
本文介绍了如何在web api 2中启用跨源请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I know its not a big issue to resolve ,But i am facing problem to consume web api in asp.net MVC web api.

I am getting below error:
"XMLHttpRequest cannot load http://192.168.1.71:800/api/IzoGlass/ToolLocationData. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:58469' is therefore not allowed access".

I've searched and find solution but not working fine with my application.

Description :-When i am consuming web api from same domain in my application its working fine.

but whenever i am consuming webapi from other domain its throwing error. given above.





我尝试过:



我在webapi.config文件中使用了这段代码。



var cors = new EnableCorsAttribute(http://192.168.1.71 ,*,*);

config.EnableCors(cors);



What I have tried:

I've used this code inside webapi.config file.

var cors = new EnableCorsAttribute("http://192.168.1.71", "*", "*");
config.EnableCors(cors);

推荐答案

在ASP.NET Web API 2中启用跨源请求ASP.NET站点 [ ^ ]



您需要启用的来源是客户端的地址,因此http:// localhost:58469在你的情况下,而不是192.168.1.71,或者你可以使用*来启用所有可能有用作测试但可能不适合生产使用的来源。但是,您不希望将webapi编码到您的开发网络服务器地址,因为这将改变。
Enabling Cross-Origin Requests in ASP.NET Web API 2 | The ASP.NET Site[^]

The origin you need to enable is the address of the client, so "http://localhost:58469" in your case, not 192.168.1.71, or you could use a * to enable all origins which might be useful as a test but maybe not ideal for production use. However you wouldn't want to code your webapi to your dev webserver address either as that will change.


这篇关于如何在web api 2中启用跨源请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆