laravel Axios/XMLHttpRequest 在生产环境中发送 GET 而不是 POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47208440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:56:33  来源:igfitidea点击:

Axios/XMLHttpRequest is sending GET instead of POST in production environment

laravelpostgetxmlhttprequestaxios

提问by Maantje

I am running into a very strange issue. We are putting an app into production and one of the POST request is turning into a POST followed directly by a GET request to the same URL and the POST is never received in the backend (Laravel). In the chrome network tab it just looks like just a GET but with Burpsuite we can see the POST request.

我遇到了一个非常奇怪的问题。我们正在将一个应用程序投入生产,其中一个 POST 请求变成了一个 POST,然后直接是一个对相同 URL 的 GET 请求,并且后端 (Laravel) 永远不会收到 POST。在 Chrome 网络选项卡中,它看起来只是一个 GET,但使用 Burpsuite 我们可以看到 POST 请求。

The code responsible

负责的代码

async store() {
    // This prints post
    console.log(this.method());

    await this.form[this.method()]('/api/admin/users/' + (this.isUpdate() ? this.id : ''));

    if (!this.isUpdate()) {
        this.form.reset();
    }
},

The form.post method content

form.post 方法内容

return new Promise((resolve, reject) => {
    axios[requestType](url, this.data())
    .then(response => {
        this.busy = false;
        this.onSuccess(response.data);
        resolve(response.data);
    })
    .catch(error => {
        this.busy = false;
        if (error.response.status == 400) {
            return this.displayErrors(error.response.data)
        }
        this.onFail(error.response.data.errors);
        reject(error.response.data);
    });
});

回答by coleander

This question was also answered by me in the Larachat slack forum, and for others sake here is the answer for the next one with such a problem.

这个问题我也在 Larachat slack 论坛上回答过,为了其他人的缘故,这里是下一个有这样问题的答案。

Just a little back story. In the chat we found out that it was receiving a 301 error which is a redirect error. I had the same error recently when posting to a url on a staging server, it was working fine locally but not on the staging server.

只是一个小小的背景故事。在聊天中,我们发现它收到了一个 301 错误,这是一个重定向错误。我最近在登台服务器上发布到 url 时遇到了同样的错误,它在本地工作正常,但在登台服务器上却没有。

The problem appeared to be a slash at the end of the post url.

问题似乎是帖子网址末尾的斜线。

So posting to https://example.com/post/to/will not work.

所以发布到https://example.com/post/to/将不起作用。

Removing the /and posting to https://example.com/post/towill work.

删除/并发布到https://example.com/post/to将工作。

回答by Nick A

Just for info, I had the same thing - axios request was being redirected. For me though, it turned out to be some localization middleware causing the redirect!

仅供参考,我有同样的事情 - axios 请求被重定向。但对我来说,结果是一些本地化中间件导致了重定向!

I set up an alternative route in the Api routes file (again Laravel as in the question), bypassing that middleware (probably where the route should have gone in the first place!). All good now! Schoolboy error I guess!

我在 Api 路由文件中设置了一个替代路由(同样是问题中的 Laravel),绕过该中间件(可能是路由应该首先到达的地方!)。现在一切都好!我猜是小学生的错误!