AJAX POST请求永远不会完成。数据发布到服务器

时间:2014-06-02 19:17:02

标签: ajax node.js post backbone.js mongoose

我正在通过AJAX发送帖子请求。数据成功发布但AJAX调用永远不会完成。前面的骨干;背面的节点。我从骨干视图和快速路径中包含了保存功能。

save: function(event) {
     event.preventDefault();

     console.log( 'You signed up for ' + this.model.get('name'));
     var name = this.model.get('name');
     var courseDay = this.model.get('courseDay');
     var time = this.model.get('time');
     var location = this.model.get('location');

        jQuery.post("/test/signups", {
          "name" : name,
          "courseDay" : courseDay,
          "time" : time,
          "location" : location,

        }, function (data, textStatus, jqXHR) { 
               console.log("Post response:");
               console.dir(data);
               console.log(textStatus);
               console.dir(jqXHR); 
        });

      }

路线:

app.post('/test/signups', isLoggedIn, function (req, res){

    User.findOne({'_id': req.user.id }, function(err, user) {
        if (err)
           return done(err);

        if (user) {
           user.signup.name = req.body.name;
           user.signup.courseDay = req.body.courseDay;
           user.signup.time = req.body.time;
           user.signup.location = req.body.location;
           user.signup.modified = req.body.modified;

           user.update({$push: { "signup" : 
            {   name: user.signup.name,
                courseDay: user.signup.courseDay,
                time: user.signup.time,
                location: user.signup.location,
                modified: user.signup.modified
            }
            }},{safe:true, upsert:true},function(err){
                if(err){
                    console.log(err);
                } else {
                    console.log("Successfully added" + user.signup);
                }
           });

           }        
        });

       });

1 个答案:

答案 0 :(得分:1)

您的服务器端代码需要发送响应。尝试类似下面的内容。注意我尝试覆盖所有错误情况,找不到用户,找到用户。

app.post('/test/signups', isLoggedIn, function (req, res){
  User.findOne({'_id': req.user.id }, function(err, user) {
    if (err) {
      return res.status(500).send(err);
    }

    if (user) {
       user.signup.name = req.body.name;
       user.signup.courseDay = req.body.courseDay;
       user.signup.time = req.body.time;
       user.signup.location = req.body.location;
       user.signup.modified = req.body.modified;

       user.update({$push: { "signup" :
         {   name: user.signup.name,
            courseDay: user.signup.courseDay,
            time: user.signup.time,
            location: user.signup.location,
            modified: user.signup.modified
         }
        }},{safe:true, upsert:true},function(err){
            if(err){
              return res.status(500).send(err);
            }
            console.log("Successfully added" + user.signup);
            res.send(user);
       });
    } else {
      res.status(404).send();
    }
  });
});