简体   繁体   English

使用JQuery更新JSON文件以执行AJAX调用node.js

[英]Updating JSON file using JQuery to perform AJAX call nodejs

I want to add a new user to a json file by using the user input in a form I am able to upload the information to a txt file using the code below but I do not know how to do this using json, any help thank you 我想通过以下形式使用用户输入将新用户添加到json文件中:我可以使用以下代码将信息上传到txt文件中,但我不知道如何使用json来完成此操作,谢谢您的帮助

ejs file: ejs文件:

<!DOCTYPE html>
<html>
<head>
<title> users </title>
</head>

<body>
  <h1>Users</h1>
  <% include templates/header.ejs %>

  <form action="http://127.0.0.1:3000/users" id="new" method="post" enctype='application/json'>
    <div id="form">
      <label for="lname">Last Name</label> <input type="text"  id="lname" name="lname">

      <label for="fname">First Name</label><input type="text"id="fname"name="fname">

      <label for="id">ID</label><input type="text"id="id"name="id">

      <button type="submit" id= "submit" value="Submit" >Submit</button>

 </form>

</body>
</html>

user.js file: user.js文件:

var express = require('express');
var router = express.Router();
var fs = require("fs");

router.get('/', function(req, res, next) {
  res.render('users');
});


//will send the user input
router.post('/', function(req, res) {

  var first= req.body.fname;
  var last= req.body.lname;
  var id= req.body.id;

  filePath = __dirname + '/people.txt';
  fs.appendFile(filePath, JSON.stringify(first + "   " + last + "   "+ id ),   function () {
    res.end();
  });

});

router.get('/listUsers', function (req, res) {
  fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {

    console.log( data );
    res.end( data );
  });
})

module.exports = router;

I would recommend against using a file to do this. 我建议不要使用文件来执行此操作。 However if you insist you can always save the data in a standard JSON format. 但是,如果您坚持要始终将数据保存为标准JSON格式。 In order to do this you can do something like: 为此,您可以执行以下操作:

var express = require('express');
var router = express.Router();
var fs = require("fs");

router.get('/', function(req, res, next) {
res.render('users');
});


 //will send the user input
 router.post('/', function(req, res) {

  var first= req.body.fname;
  var last= req.body.lname;
  var id= req.body.id;

   var person = {
     first: first,
     last: last,
     id: id
   }; // if you are using es6 you do var person = {first, last, id}

  var filePath = __dirname + '/people.json';
  fs.readFile(filePath, function (err, file) {
    if (err) {
      res.status = 500;
      res.send(err).end();
      return;
    }

    var fileString = file.toString();
    var people = fileString ? JSON.parse(fileString) : [];
    people.push(person);
    fs.writeFile(filePath, JSON.stringify(people), function(err) {
       if (err) {
        res.status = 500;
        res.send(err).end();
        return;
      }

      res.end();
    });
  });
});



router.get('/listUsers', function (req, res) {
  fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
    if (err) {
      res.status = 500;
      res.send(err).end();
      return;
    }
    res.json(JSON.parse(data));
  });
})



module.exports = router;

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

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