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");
            });
         });
    
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊