1

我已将此 JavaScript 放入我的 HTML 代码中,以尝试delete向我的页面添加一个功能按钮。我已经测试了脚本是否可以识别单击并且确实可以,但是它没有通过我的删除路线,目前我只是在测试路线所以只在其中放置了一个简单的控制台日志但是在这里是代码。有任何想法吗?

script(type = 'text/javascript')
 $('#teamDelete').live('click', function () {
  var teamId = #{teamData.key};
  $.del('/team/' + teamId, function (response) {
    console.log(response);
  });
});

路线:

app.del('/team/:key', function(req, res) {
  util.log('Serving request for url [DEL] ' + req.route.path);
  util.log('success');
});

应用程序:

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.enable('jsonp callback');
  app.set('view engine', 'jade');
  app.set('view options', {layout : false});
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({
    secret : 'secretsecret'
  }));
  app.use(express.methodOverride());
  app.use(express.static(__dirname + '/public'));
  app.use(function(req, res, next){
    res.locals.session = req.session;
    next();
  });
  app.use(app.router);
});

/团队

    app.get('/team'/*, lim("Must be logged in to see teams")*/, getAllTeams, function(req, res){
    util.log('Serving request for url [GET] ' + req.route.path);
    // Pass it the list of all Teams
    res.render('team', {'teamsList' : req.teamsList} );
  });

  /**
    * POST /team
    * Save new Team
    */
  app.post('/team', function(req, res) {
    util.log('Serving request for url [POST] ' + req.route.path);

    // Output to console the contents of req.body
    // console.log('body: ', req.body);
    // console.log('body.teamForm: ', req.body.teamForm);
    // console.log('body.teamForm.name: ', req.body.teamForm.name);
    // console.log('body.teamForm.teamKey: ', req.body.teamForm.teamKey);

    // Get data from teamForm
    var teamForm = req.body.teamForm;

    // Save team in teamForm as new Team
    var name = teamForm.name;
    var team = new Team();
    team.name = name;

    // Save new Team to datbase
    team.save(function(err){
      var message = '';
      var retStatus = '';
      // No error - Successful Save
      if(!err){
        util.log('Successfully created new team: ' + name);
        message = 'Successfully created new team: ' + name;
        retStatus = 'success';
      } 
      // Error - Unsuccessful Save, show error
      else {
        util.log('Error while creating team: ' + name + ' error : ' + util.inspect(err));
        if(err.code === 11000){
          message = 'Team already exists';
        }
        retStatus = 'failure';
      }
      // Return whether the Save was successful
      res.json({
        'retStatus' : retStatus,
        'message' : message
      });
    });
  });

  /**
    * GET /team/:key
    * Get Team details by key
    */
  app.get('/team/:key', function(req, res) {
    util.log('Serving request for url [GET] ' + req.route.path);
    Team.findByKey(req.params.key, function(err, teamData){
      if(!err && teamData){
        teamData = teamData;
        res.render('teamDetails', { 'teamData' : teamData } );
      } else {
        util.log('Error in fetching Team by key : ' + req.params.key);
        res.json({
          'retStatus' : 'failure',
          'msg' : 'Error in fetching Team by key ' + req.params.key
        });
      }
    });
  });

  /**
    * DEL /team/:key
    * Delete Team by key
    */
  app.del('/team/:key', function(req, res) {
    util.log('Serving request for url [DEL] ' + req.route.path);

    util.log(req.params.key);

    Team.remove(req.params.key, function(err){
      var message = '';
      var retStatus = '';
      if (!err) {
        util.log('Successfully deleting Team with key: ' + req.params.key);
        message = 'Successfully deleting Team with key: ' + req.params.key;
        retStatus = 'Success';
      } else {
        util.log('Error deleting Team with key: ' + req.params.key + 'Error: ' + util.inspect(err));
        res.json({
          'retStatus' : 'failure',
          'msg' : 'Error in fetching Team with key ' + req.params.key
        });
      }
    });
  });
4

1 回答 1

1

$.del()不存在。您可以使用 生成删除请求$.ajax,但使用更安全$.post()

$.post('/team/' + teamId, { _method : 'delete' }, function(response) {
  console.log(response);
});

为此,您还需要methodOverride在 Express 中使用中间件(定义app.del路由之前):

app.use(express.bodyParser()); // this has to come before the next line
app.use(express.methodOverride());
于 2013-03-09T15:29:58.500 回答