简体   繁体   English

Axios POST 请求将数据发送到 Express 服务器但错误 404

[英]Axios POST request sends data to Express server but Error 404

Axios POST request sends data to Express sever but Error 404 Axios POST 请求将数据发送到 Express 服务器但错误 404

Hello, world, I am trying to build a user authentication server for a project I am working on, but I am running into a problem trying to send a POST request to my Node.js Express server.你好,世界,我正在尝试为我正在处理的项目构建用户身份验证服务器,但在尝试向我的 Node.js Express 服务器发送 POST 请求时遇到问题。

I want to send a POST request using Axios containing a username and password from the browser.我想使用 Axios 发送一个 POST 请求,其中包含来自浏览器的用户名和密码。 But once sending the request it gives me a 404 Not Found error.但是一旦发送请求,它就会给我一个404 Not Found错误。 The request has to go to http://website/api/login and my Node.js code should return either "authed" or "invalid".请求必须从 go 到http://website/api/login和我的 Node.js 代码应该返回“authed”或“invalid”。 I tested the API inside Postman and that seems to be working.我在 Postman 内测试了 API ,这似乎工作正常。 I also exported the request code from Postman and tested it with fetch API , xhr , and Axios , all returning the same result.我还从 Postman 导出了请求代码,并使用fetch APIxhrAxios对其进行了测试,都返回了相同的结果。

The server receives the data and handles it properly, but when I look in the Chromium debugger it appears that the request URL is just http://website/ and not http://website/api/login .服务器接收数据并正确处理它,但是当我查看 Chromium 调试器时,似乎请求 URL只是http://website/而不是http://website/api/login I am honestly lost and I have tried what feels like everything, but I can't seem to make it work.老实说,我迷路了,我尝试了一切感觉,但我似乎无法让它发挥作用。 Any help in pointing me in the right direction would be amazing!任何能帮助我指出正确方向的帮助都会很棒! Thank you!谢谢!

The code I use for the POST request is:我用于 POST 请求的代码是:

    const username = document.getElementById("username").value;
        const password = document.getElementById("password").value;

        const data = JSON.stringify({"username": username, "password":password});

        const config = {
            method: 'post',
            url: 'http://website/api/login',
            headers: {
                'Content-Type': 'application/json'
            },
            data : data
        };

        axios(config).then(function (response) {
            console.log(JSON.stringify(response.data));
        }).catch(function (err) {
            console.log(err);
        })
    }

This is what I see in the Chromium debugger: Headers这是我在 Chromium 调试器中看到的: Headers

This is my Node.js / Express code:这是我的 Node.js / 快递代码:

app.post('/api/login', function (req, res, next) {
    scriptFile.authUser(req.body, function (err, state) {
        if (err) console.log(err);
        else {
            if (state) {
                res.send("authed");
            } else {
                res.send("invalid");
            }
        }
    });
})

Thank you for any help I can get.感谢您为我提供的任何帮助。

I am stupid,我很蠢,

Breakdown of what happened:发生的事情的细分:

Everything was working fine except that I put the input data and submit button inside a form, which will refresh the page...一切正常,除了我将输入数据和提交按钮放在表单中,这将刷新页面......

I fixed it by changing the form to a div.我通过将表单更改为 div 来修复它。

Hey checking your chrome console pic looks like your post request is hitting the root api address 'http://website/' and not the full path 'http://website/api/login嘿,检查您的 chrome 控制台图片看起来您的帖子请求正在访问根 api 地址“http://website/”而不是完整路径“http://website/api/login”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM