我有客户端和服务器在不同的端口上运行.服务器正在运行Web API 2(v5.0.0-rc1).
我尝试安装Microsoft ASP.NET Web API跨源支持包并启用它WebApiConfig.cs.它给了我EnableCors()功能,所以包正确安装.
在这里你可以看到我的Register()功能WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
}
Run Code Online (Sandbox Code Playgroud)
GET请求工作正常.但在发送时POST,我得到以下内容:
OPTIONS http://localhost:19357/api/v1/rooms? 404 (Not Found) angular.js:10159
OPTIONS http://localhost:19357/api/v1/rooms? Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin. angular.js:10159
XMLHttpRequest cannot load http://localhost:19357/api/v1/rooms. Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
根据Fiddler的说法,它只发送OPTIONS请求.POST之后不发布.
所以我猜测config.EnableCors(cors);在WebApiConfig.cs没有做任何事情,这导致服务器拒绝客户端/浏览器发送POST请求.
你知道如何解决这个问题吗? …