我最近在我的Web控制器中实现了cors。我从domain1上的客户端调用domain2上的web。
对于原点,我指定了一个假的url。据我所知,只有这个网址的电话才会被接受。这是正确的吗?
所以只有从
http://notgoingtowork.com将能够调用控制器并返回数据。
这是我的控制器(domain1)
public class TestController : ApiController
{
[EnableCors(origins: "http://notgoingtowork.com", headers: "*", methods: "*")]
public int Get()
{
return 1
}
}然后在我的另一个域,ajax调用(domain2)
$.ajax({
url: "http://domain1/api/Test/Get",
method: "GET",
headers: { "accept": "application/json;odata=verbose" },
success: function (data){alert("it worked");},
error: function (error) { alert("Did not work"); }
})但是,请求仍然成功,并且正在返回数据。由于客户端不在“http://notgoingtowork.com”上,它如何才能成功地做到这一点?我遗漏了什么?
我在IE11上做这件事。这在镀铬上很好。
编辑:这里的是WebApiConfig.cs文件,它非常通用。我刚加了Cors的部分
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableCors();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}发布于 2014-10-08 06:12:08
在IE 11中有一个设置“跨域访问数据源”,确保它是否被禁用:

https://stackoverflow.com/questions/26244059
复制相似问题