Javascript 尝试使用主体解析器从ExpressV4中的POST请求解析JSON对象
我目前正在自学更多关于服务器代码的知识,特别是使用Node.js和Express,在接收和解析POST请求发送的JSON对象时遇到了很多麻烦。我看了很多其他的帖子(链接到下面),我一辈子都搞不清楚到底出了什么问题。以下是我所看到的: Javascript:使用AJAX发送JSON对象 如何在Express应用程序中使用JSON POST数据 使用XMLHttpRequest发送POST数据 如何在Node.js中提取POST数据? 所有这些都使我走上了正确的道路,但我还没有完全做到,因此我正在寻求帮助。以下是我正在使用的代码: 发送POST请求Javascript 尝试使用主体解析器从ExpressV4中的POST请求解析JSON对象,javascript,node.js,express,xmlhttprequest,body-parser,Javascript,Node.js,Express,Xmlhttprequest,Body Parser,我目前正在自学更多关于服务器代码的知识,特别是使用Node.js和Express,在接收和解析POST请求发送的JSON对象时遇到了很多麻烦。我看了很多其他的帖子(链接到下面),我一辈子都搞不清楚到底出了什么问题。以下是我所看到的: Javascript:使用AJAX发送JSON对象 如何在Express应用程序中使用JSON POST数据 使用XMLHttpRequest发送POST数据 如何在Node.js中提取POST数据? 所有这些都使我走上了正确的道路,但我还没有完全做到,因此我正在
var button = document.querySelector("#button");
button.onclick = function(){
console.log("Getting data from local server");
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/data/test.json", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(JSON.stringify({"latitude": 41.2418, "longitude": -70.8898}));
};
在服务器中处理POST请求
var http = require("http");
var fs = require("fs");
var express = require("express");
var app = express();
var path = require("path");
var bodyParser = require("body-parser");
var port = process.env.PORT || 3000;
//tells express where to find all the static files (HTML, CSS, etc) and load them into the browser
app.use(express.static(path.join(__dirname, '../client')));
//tells the application to use body-parser as middleware so it can handle post requests
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//routing methods
//deal with incoming GET and POST requests to the server
app.get("/", function(req, res){
res.send("Submitted GET Request");
})
//only handles incoming POST requests to the test.json resource
app.post("/data/test.json", function(req, res){
console.info("Submitting POST Request to Server");
console.info("Request body: " + req.body);
//write the file
fs.writeFile(__dirname + "/../client/data/test.json", req.body,
function(err){
if(err){
console.error(err); //print out the error in case there is one
return res.status(500).json(err);
}
//resolve the request with the client
console.info("updated test.json");
res.send();
});
})
//tell the express object to create the server and listen on the port
app.listen(port);
console.log("Listening on localhost:" + port);
每当我试图打印出“req.body”的内容时,就会得到“[object]”的输出。有什么想法吗
编辑:
我的问题已经解决了。我变了
console.info("Request body: " + req.body);
到
我还将POST XMLHTTPRequest中的内容类型更改为“application/json”,以帮助格式化。如果您的帖子正文预期为json,请更改此行
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
到
“[object object]”
是JavaScript隐式toString
操作的默认结果,它在尝试将该对象的字符串表示形式写入文件时使用该操作
尝试将JSON.stringify(req.data)
写入文件
此外,在客户端-考虑更改<代码>内容类型< /代码>标题以匹配:
xhr.setRequestHeader(“内容类型”、“应用程序/json”)代码>嗯。。。您的内容类型与数据不匹配???试试这个JSON.stringify(req.body);[object object]表示您试图打印的变量不是字符串,简单的修复方法是使用JSON.stringify(req.body)@KevinB我认为我可以在查看不同帖子的基础上互换使用“application/json”和“application/x-www-form-urlencoded”,但这肯定是问题的一部分。这对[object]问题没有帮助,但修复了我的json对象的格式。
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-Type", "application/json");