【发布时间】:2018-11-26 13:17:16
【问题描述】:
我有一些奇怪的东西,我正在尝试解决问题。我的目标是在我的 Fetch 调用中有AntiForgeryToken。从昨天开始我一直在努力,但我遇到了麻烦。
在我的 React 应用程序中,我有一个 Fetch 调用:
fetch('/comments/new?',
{
method: 'POST',
body: JSON.stringify({ data }), // data can be `string` or {object}!
headers: { 'Content-Type': 'application/json' },
contentType: "application/json; charset=utf-8",
credentials: 'include'
}
)
.then(res => res.json())
.then(res => {
console.log(res);
})
.catch(error => {
console.error(error);
});
而我的SurveyController.cs 看起来像:
[Route("comments/new")]
public ActionResult AddComment(Survey survey)
{
if (survey == null)
{
return BadRequest();
}
survey.Time = DateTime.Now;
_context.Surveys.Add(survey);
_context.SaveChanges();
return Ok();
}
它可以工作,并且在 XHR 日志获取调用的 URL 中我有:http://localhost:58256/comments/new?Name=aaa&Change=aa&Opinion=good
但是,如果我在 XHR 日志中将 Fetch URL 更改为 fetch('/comments/new/dupa?', 并将构造函数 Route URl 更改为 [Route("comments/new/dupa")],则再次调用相同(!):http://localhost:58256/comments/new?Name=aaa&Change=aa&Opinion=good
另一个奇怪的事情是 XHR 日志将呼叫识别为GET。
我不明白为什么。我非常感谢任何帮助。
【问题讨论】:
标签: c# ajax reactjs asp.net-core-mvc fetch