我在客户端有以下代码:
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 中工作。
慕莱坞森
相关分类