[英]Redirecting to subdomain from WebAPI controller - CORS required?
Currently, my WebAPI controller does this: 当前,我的WebAPI控制器执行以下操作:
var newUrl = _adminService.Foo(bar);
return Request.CreateResponse(HttpStatusCode.OK, newUrl);
So the client receives a string, and then sets window.location
with javascript. 因此,客户端收到一个字符串,然后使用javascript设置window.location
。
This seems like a hacky way to redirect via WebAPI. 这似乎是通过WebAPI重定向的一种不可靠的方法。 I found this post: 我发现了这篇文章:
Redirect from asp.net web api post action 从ASP.NET Web API重定向操作
Which I've implemented: 我已经实现的:
var newUrl = _adminService.Foo(bar);
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri(newUrl);
return response;
But now I get the following error in chrome (when navigating from localhost to the subdomain): 但是现在我在chrome中遇到以下错误(从本地主机导航到子域时):
XMLHttpRequest cannot load http://test.localhost:3806/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3806' is therefore not allowed access.
Do I need to configure CORS for this? 我是否需要为此配置CORS?
Do I need to configure CORS for this? 我是否需要为此配置CORS?
Yes, you do need if your javascript is hosted on a different domain than your Web API. 是的,如果您的JavaScript托管在与Web API不同的域上,则确实需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.