首页 > 解决方案 > 为什么我在 React 应用程序中的 Fetch URL 没有改变?

问题描述

我有一些奇怪的东西,我正在尝试解决问题。我的目标是AntiForgeryToken在我的 Fetch 调用中。我从昨天开始一直在努力,但我遇到了麻烦。

在我的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

另一个奇怪的事情是GETXHR 日志将呼叫识别为。

我不懂为什么。我很感激任何帮助。

标签: c#ajaxreactjsasp.net-core-mvcfetch

解决方案


这是我愚蠢的错误。因为我已经有几个月没有在 React 中编码了,而且我只是忘记了在npm start我保存更改的任何时候构建应用程序。


推荐阅读