weixin_33701294 2016-12-10 16:44 采纳率: 0%
浏览 47

重新发送无法正常工作

I am working with a simple CRUD app with jquery ajax and node.js, just to improve my skills with node and ajax. The thing is that I am doing a post request that is handled with my post router in the node server, and everything is working fine. It adds 1 more product to my products.json file, but in the end it doesn't send the response back to the client, the final res.send("done") doesn't work and I don't know why..

here is the code:

ajax

$("#create-form").on('submit',function(){
    event.preventDefault();
    var createIn = $("#create-input").val();
    $.ajax({
        url: '/products',
        method:'POST',
        data:JSON.stringify({name:createIn}),
        contentType: "application/json",
        dataType: "json",
        success: function(data){
          console.log(data);
          $("create-input").val("");
          $("get-button").click(); 
        }
    });
})

node

app.post('/products',function(req,res){
    fs.readFile('products.json','utf8',function(err,data){
        var result = JSON.parse(data);
        var productName = req.body.name;
        console.log(req.body.name);
        currentId++;
        var productId = currentId;
        var product = {
            name: productName,
            id: productId
        }
        result.products.push(product);
        fs.writeFile(__dirname + "/products.json",JSON.stringify(result),'utf8');
     });
    res.send("post done");
});

This is just the important part of the code, it works and just fails at the end in the res.send.

  • 写回答

2条回答 默认 最新

  • weixin_33699914 2016-12-10 16:53
    关注

    This does not answer your question directly but you should ideally not send back the response until you know the work has been done, and you should handle errors. In other words you should use the callbacks. (Too many callbacks can be problematic and you should investigate other patterns - eg promises - bit no need here)

    app.post('/products',function(req,res){
        fs.readFile('products.json','utf8',function(err,data){
            if (err) return res.send("error");
            var result = JSON.parse(data);
            var productName = req.body.name;
            console.log(req.body.name);
            currentId++;
            var productId = currentId;
            var product = {
                name: productName,
                id: productId
            }
            result.products.push(product);
            fs.writeFile(__dirname + "/products.json",JSON.stringify(result),'utf8', function(err, res) {
             if (err) return res.send("error");
             res.send("post done");
            });
         });
    
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容