【发布时间】:2018-11-08 07:09:24
【问题描述】:
我在客户端有以下代码:
fetch("/music/index", { headers: { "Content-Type": "application/json" } })
.then(response => {
if (!response.ok) {
throw response;
}
return response.json();
})
.then(json => {
console.log("Done! It's all good");
})
.catch(response => console.log(response));
不幸的是,这甚至没有到达MusicController(服务器端),它看起来如下(简化以说明这一点):
[Authorize]
public class MusicController : Controller {
public async Task<IActionResult> Index() {
IEnumerable<Song> songs = await _songsRepository.GetAll();
return Json(songs);
}
}
根据我在开发者控制台中看到的内容,我被重定向到 /Account/Login?returnUrl...
同时,使用 jquery api,一切似乎都正常:
$.get("/music/index")
.done(json => console.log("Done! It's all good"))
.fail(error => console.log(error));
我怀疑我的标题设置不正确?不确定在网上找不到任何东西。此外,这个(或者说非常相似的)代码用于以前(非核心)版本的 ASP.NET。
【问题讨论】:
-
您是否尝试过添加
credentials: 'include'选项? developer.mozilla.org/en-US/docs/Web/API/Request/credentials -
@GetOffMyLawn 伙计,你太棒了,我什至不知道这件事 - 它奏效了。你能把它写成答案吗?
-
@RoryMcCrossan 我在任何地方都没有
$.ajaxSetup()电话,我认为默认设置是为了让它工作。
标签: javascript c# jquery asp.net-core asp.net-core-2.0