C# 为什么我在React应用程序中的获取URL没有更改?
我有一些奇怪的事情,我正试图解决。我的目标是在Fetch调用中使用C# 为什么我在React应用程序中的获取URL没有更改?,c#,ajax,reactjs,asp.net-core-mvc,fetch,C#,Ajax,Reactjs,Asp.net Core Mvc,Fetch,我有一些奇怪的事情,我正试图解决。我的目标是在Fetch调用中使用AntiForgeryToken。从昨天起我就一直在做这件事,但我面临着一个麻烦 在我的React应用程序中,我有一个获取调用: fetch('/comments/new?', { method: 'POST', body: JSON.stringify({ data })
AntiForgeryToken
。从昨天起我就一直在做这件事,但我面临着一个麻烦
在我的React
应用程序中,我有一个获取调用:
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
但是,如果我将Fetch URL更改为Fetch('/comments/new/dupa?'),
和构造函数将URL路由到我的XHR日志中的[Route(“comments/new/dupa”)]
,Fetch调用也是一样的(!):http://localhost:58256/comments/new?Name=aaa&Change=aa&Opinion=good
另一件奇怪的事情是,XHR日志将调用识别为GET
我不明白为什么。我非常感谢任何帮助。这是我愚蠢的错误。因为我有几个月没有在React中编写代码,我只是忘记了在保存更改时使用
npm start
构建应用程序