在节点JS服务器中使用提取请求时,它会接收到身体为空白

发布于 2025-01-18 16:25:21 字数 1262 浏览 1 评论 0原文

我正在节点JS中的用户登录系统工作,并正在向服务器提出发布请求。

let data = {
    username: "John Doe",
    password: "123abc",
}

let options = {
    method: 'POST',
    headers: {
        "Content-type": "application/json"
    },
    body: JSON.stringify(data),
}

fetch('/verify-login', options).then(function(r) {
    return r.text();
}).then(function(dat) {
    if (dat == 'n') {
        document.getElementById('login-fail').innerHTML = 'User name or password is incorrect!';
    } else {
        console.log('Login Success');
    }
});

服务器端代码:

const express = require('express');
const port = 80;
const bodyParser = require("body-parser");
const fs = require('fs');

var app = express();

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const cors = require("cors");
app.use(cors());

app.post('/verify-login', async function(q, r) {
    let dat = await q.body; //<-- Body is just {} not what the fetch request sent

    //do account check stuff with dat

    if (success) {
        r.send('y');
    } else {
        r.send('n');
    }
});

app.listen(port, function() {
    console.log("Started application on port %d", port);
});

此问题是在我接收请求时在服务器端,将带有'{}'返回正文。有人知道为什么会发生这种情况以及如何解决吗?

I was working on a user login system in Node JS and was making a POST request to the server like this.

let data = {
    username: "John Doe",
    password: "123abc",
}

let options = {
    method: 'POST',
    headers: {
        "Content-type": "application/json"
    },
    body: JSON.stringify(data),
}

fetch('/verify-login', options).then(function(r) {
    return r.text();
}).then(function(dat) {
    if (dat == 'n') {
        document.getElementById('login-fail').innerHTML = 'User name or password is incorrect!';
    } else {
        console.log('Login Success');
    }
});

Server Side code:

const express = require('express');
const port = 80;
const bodyParser = require("body-parser");
const fs = require('fs');

var app = express();

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const cors = require("cors");
app.use(cors());

app.post('/verify-login', async function(q, r) {
    let dat = await q.body; //<-- Body is just {} not what the fetch request sent

    //do account check stuff with dat

    if (success) {
        r.send('y');
    } else {
        r.send('n');
    }
});

app.listen(port, function() {
    console.log("Started application on port %d", port);
});

This issue is that on the server side when I receive the request, the body is returned with '{}'. Does anybody know why this is happening and how I can fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-01-25 16:25:21

您可以通过body选项将各种数据类型传递给fetch

如果您传递了无法识别的东西,它将其转换为字符串并发送。

将普通的对象转换为字符串并不能为您提供任何有用的东西。

let data = {
    username: "John Doe",
    password: "123abc",
}

console.log(`${data}`);

您说您正在发送JSON(使用content-type标题。因此,您需要实际发送JSON。

const json = JSON.stringify(data);

There are various data types you can pass to fetch through the body option.

If you pass something it doesn't recognise, it converts it to a string and sends that.

Converting a plain object to a string doesn't give you anything useful.

let data = {
    username: "John Doe",
    password: "123abc",
}

console.log(`${data}`);

You said you were sending JSON (with the Content-Type header. So you need to actually send JSON.

const json = JSON.stringify(data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文