【问题标题】:Node.JS post to JSON file not formatted correctlyNode.JS 发布到 JSON 文件的格式不正确
【发布时间】:2016-12-18 17:12:48
【问题描述】:

我的 ajax 到 node.js 发布到 JSON 文件时遇到问题。它可以正常工作,但是我假设它的格式不正确。因为当它读取数据时,它不会回来,因为我认为它认为它不可读。

这是 AJAX 调用:

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

这里是 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/');

这是写入文件的方式:

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

这是我的 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
}
]

【问题讨论】:

  • commentJSON 究竟包含什么?
  • @KevinB 这是一个 JQuery 插件,所以它应该读取和写入它自己的数据我假设viima.github.io/jquery-comments
  • 将字符串化添加到 ajax 中的数据似乎有所帮助。但是,它不是作为数组出现的。它还不断覆盖文件中的数据而不是添加到文件中
  • 听起来你有多个问题。

标签: javascript jquery json ajax node.js


【解决方案1】:

将我的 ajax 调用更改为此使其对我的 get 请求可读。但它正在覆盖 JSON 文件中的数据。我希望它只是添加到它。

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

【讨论】:

  • 也许你应该编辑你的问题,把它缩小到关于附加到文件的部分。
猜你喜欢
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 2020-11-14
  • 1970-01-01
相关资源
最近更新 更多