【发布时间】: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