gpt4 book ai didi

c# - 从 WebAPI Controller 重定向到子域 - 需要 CORS?

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:57 25 4
gpt4 key购买 nike

目前,我的 WebAPI Controller 执行此操作:

var newUrl = _adminService.Foo(bar);
return Request.CreateResponse(HttpStatusCode.OK, newUrl);

于是客户端接收到一个字符串,然后用javascript设置window.location

这似乎是一种通过 WebAPI 重定向的 hacky 方式。我找到了这篇文章:

Redirect from asp.net web api post action

我已经实现了:

var newUrl = _adminService.Foo(bar);
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri(newUrl);
return response;

但现在我在 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.  

我需要为此配置 CORS 吗?

最佳答案

Do I need to configure CORS for this?

是的,如果您的 javascript 托管在与 Web API 不同的域中,您确实需要。

关于c# - 从 WebAPI Controller 重定向到子域 - 需要 CORS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20637799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com