2017-06-05 27 views
1

我目前正在教我自己更多關於服務器代碼,特別是使用Node.js和Express,並且我有很多接收和解析的麻煩從POST請求發送的JSON對象。我看了很多其他的帖子(鏈接到下面),我無法弄清楚我的生活出了什麼問題。以下是我已經看了:試圖解析JSON對象從POST請求在Express v4使用身體分析器

的Javascript:發送JSON對象與AJAX Javascript : Send JSON Object with Ajax? 如何消耗JSON POST數據的快速應用 How do I consume the JSON POST data in an Express application 使用XMLHttpRequest的 Send POST data using XMLHttpRequest 發送POST數據如何提取POST Node.js中的數據? How do you extract POST data in 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 Object]「的輸出。有任何想法嗎?編號: 我的問題已解決。我已經改變

console.info("Request body: " + req.body); 

console.info("Request body: " + JSON.stringify(req.body)); 

我也改變了我的Content-Type在我的崗位XMLHttpRequest來 「應用/ JSON」,以幫助格式。

+1

呃...您的內容類型不匹配您的數據... ??? –

+1

試試這個JSON.stringify(req.body); – VivekN

+0

[對象對象]意味着你想要打印的變量不是字符串,只需使用JSON.stringify(req.body); –

回答

2

"[object Object]"是JavaScript的隱含toString操作,它嘗試時使用的默認結果將該對象的字符串表示寫入該文件。

嘗試將JSON.stringify(req.data)改爲寫入文件。

此外,在客戶端 - 考慮改變你的Content-Type頭匹配:

xhr.setRequestHeader("Content-Type", "application/json");

+0

這解決了我的問題!謝謝。 –

-1

如果您的帖子體預計將JSON,然後改變這一行

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 

xhr.setRequestHeader("Content-Type", "application/json"); 
+0

沒有幫助[對象對象]問題,但修復了我的JSON對象的格式。 –