【问题标题】:Sending JSON from Client JS, Server Nodejs Reads as [object object]从客户端 JS 发送 JSON,服务器 Nodejs 读取为 [object object]
【发布时间】:2019-04-30 12:00:54
【问题描述】:

您好,我只是想通过 POST 更新欢迎消息,据我所知,我正在从客户端 JavaScript 发送 JSON,在 NodeJS 端它显示为 [object object ] 我试过 req.body 并返回“未定义”。我想知道如何从我发送到 nodejs 服务器的 JSON 中提取我的欢迎消息并保存到 .JSON 以便以后能够从客户端中提取。

我已经尝试过 jsonstringify(req) 并在我的 nodejs cmd 中返回一个大错误,如果有必要我可以粘贴它。

nodejs 服务器 POST,它会写入文件welcome.json,它会写入[object object]undefined,这取决于我是使用req.body 还是req

app.post('/update', function (req, res) {
    fs.writeFile(__dirname + '/rqsts/welcome.json', req.body, function () {
        console.log('We got a Post request' + req.body);
    });
});

这是我的客户端 http post 请求:

function submit() {
    var text_Input = document.getElementById('textinput').value;
    var testing = document.getElementById('testme');
    var welcome_array = {
        welcome: ""
    };
    welcome_array.welcome = text_Input;
    var welcomeJSON = JSON.stringify(welcome_array);
    var url = 'http://localhost:8080/update';
    var http = new XMLHttpRequest();
    http.open('POST', url, false); // false for synchronous request
    Add_Para(welcomeJSON, testing);
    http.send(welcomeJSON);
}

Add_Para 是我为解决问题而制作的一个函数,它使用请求的数据“welcomeJSON”在所述 html 中添加一个段落

【问题讨论】:

  • 我在尝试 JSON.stringify(req) TypeError: Converting circular structure to JSON 时收到这个

标签: javascript node.js post


【解决方案1】:

如果您使用的是 expres 4.16 或更高版本,请使用

app.use(express.json());

不用bodyParser

【讨论】:

  • 就我的依赖项而言,我在 4.14 上。我可以将我的 package.json 更新到 4.16 吗?
  • 你说得对,我更新了我的版本并制作了我的源代码来适应这个。谢谢。
  • 不客气,如果你觉得它有效,请接受答案。@DevinB
【解决方案2】:

试试

console.log('We got a Post request %O', req.body);

参考:https://developer.mozilla.org/en-US/docs/Web/API/Console

例子:

const a = {
  a: 'a',
};
console.log('We got a Post request %O', a); // We got a Post request { a: 'a' }

或者你可以尝试使用 JSON.stringify

例子:

const a = {
  a: 'a',
};
console.log('We got a Post request ' + JSON.stringify(a)); // We got a Post request {"a":"a"}

【讨论】:

  • 我们有一个未定义的 Post 请求
  • 它正在正确记录变量,我似乎无法将此对象转换为 JSON,即使它在发送之前已被转换为 JSON。
  • 你可以先打印出 req 看看里面有什么。至少它不会记录[object object]来帮助您解决问题。
  • 您的意思是在 Nodejs 端制作 json 数组并将其附加到传入的请求中吗?
  • 当我尝试 req 它是 [object object] req.body 返回 undefined
【解决方案3】:

您好,我找到了我的解决方案,我相信我遗漏了包括 body-parser 是主要问题,这是我现在更新的代码。

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.post('/update', function (req, res) {
    console.log('We got a Post request "%O"', req.body.welcome);
    var now = new Date();
    console.log('Time of change: ' + now.getHours() + ":" + now.getMinutes() + " " + now.getDate() + "/" + now.getMonth() + "/" + now.getFullYear());
    fs.writeFile(__dirname + '/rqsts/welcome.json', JSON.stringify(req.body), function () {
    });
});

客户端js

function submit() {
    var text_Input = document.getElementById('textinput').value;
    var testing = document.getElementById('testme');
    var welcome_array = {
        "welcome": ""
    };
    welcome_array.welcome = text_Input;
    var welcomeJSON = JSON.stringify(welcome_array);
    var url = 'http://localhost:8080/update';
    var http = new XMLHttpRequest();
    http.open('POST', url, false);
    http.setRequestHeader('Content-type', 'application/json')
    Add_Para(welcomeJSON, testing);
    http.send(welcomeJSON);
}

【讨论】:

    【解决方案4】:

    你是否在 express 中使用“body-parser”来读取 req.body?

    Body-parser 将帮助您提取 req.body 内容,请点击以下链接了解如何在 Node JS 中使用。 https://blog.fullstacktraining.com/how-do-you-extract-post-data-in-node-js/

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 2022-01-08
      • 2022-01-12
      • 2023-03-30
      • 2020-02-28
      • 2012-10-25
      • 2015-10-22
      • 2020-08-17
      • 2015-03-05
      相关资源
      最近更新 更多