简体   繁体   English

Node.JS发布到JSON文件的格式不正确

[英]Node.JS post to JSON file not formatted correctly

I am having issues with my ajax to node.js post to JSON file. 我在将node.js的ajax发布到JSON文件时遇到问题。 It works properly, however it is not formatted properly I am assuming. 它可以正常工作,但是我认为它的格式不正确。 Because when it reads the data, it does not come back due to what I assume is it thinks it is unreadable. 因为当我读取数据时,由于我认为它不可读而无法返回。

Here is the AJAX call: 这是AJAX调用:

postComment: function(commentJSON, success, error) {
  $.ajax({
    type: 'post',
    url: 'http://localhost:8080',
    data: commentJSON,
    success: function(comment) {
      success(comment)
    },
    error: error
  });
},

Here is the NODE.JS : 这是NODE.JS

var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {

  console.log('Request received: ');
  if (req.method == 'POST') {
    req.on('data', function(chunk) {
      console.log(data);
      fs.writeFile("comments-data.json", chunk, function(err) {
        if (err) {
          return console.log(err);
        }

        console.log("The file was saved!");
      })
    });
    res.end('{"msg": "success"}');
  };
  if (req.method == 'GET') {
    fs.readFile("comments-data.json", 'utf8', function(err, data) {
      if (err) {
        return console.log(err);
      } else {
        res.setHeader("Content-Type", "text/json");
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.end(data)
      }
    })
  };
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');

Here is how it writes to the file: 它是如何写入文件的:

id=c1&parent=&created=2016-08-11T19%3A24%3A31.418Z&modified=2016-08-11T19%3A24%3A31.418Z&content=test&fullname=&profile_picture_url=https%3A%2F%2Fviima-app.s3.amazonaws.com%2Fmedia%2Fuser_profiles%2Fuser-icon.png&created_by_current_user=true&upvote_count=0&user_has_upvoted=false

Here is what my get request understands: 这是我的get请求了解的内容:

[
{  
"id": 1,
"parent": null,
"created": "2015-01-01",
"modified": "2015-01-01",
"content": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed posuere interdum sem. Quisque ligula eros ullamcorper quis, lacinia quis facilisis sed sapien. Mauris varius diam vitae arcu.",
"fullname": "Simon Powell",
"profile_picture_url": "https://app.viima.com/static/media/user_profiles/user-icon.png",
"created_by_admin": false,
"created_by_current_user": false,
"upvote_count": 3,
"user_has_upvoted": false
}
]

Changing my ajax call to this made it readable for my get request. 将我的ajax调用更改为此使其对于我的get请求可读。 But it is overwriting the data in the JSON file. 但这会覆盖JSON文件中的数据。 I would like it to just add to it. 我希望添加它。

                postComment: function(commentJSON, success, error) {
                        $.ajax({
                            type: 'post',
                            url: 'http://localhost:8080',
                            data: JSON.stringify([commentJSON]),
                            success: function(comment) {
                                success(comment)
                            },
                            error: error
                        });
                },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM