我已将此JavaScript放入我的HTML代码中,尝试在我的页面中添加一个正常运行的delete
按钮。我已经测试了脚本teset,如果它识别出点击它确实如此,但它没有进入我的删除路由,此刻我只是测试路由所以只放了一个简单的控制台日志但是这里是代码。有什么想法吗?
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
});
}
});
});
答案 0 :(得分:1)
$.del()
不存在。您可以使用$.ajax
生成DELETE请求,但使用$.post()
更安全:
$.post('/team/' + teamId, { _method : 'delete' }, function(response) {
console.log(response);
});
要实现此目的,您还需要在Express中使用methodOverride
中间件(在定义app.del
路由之前):
app.use(express.bodyParser()); // this has to come before the next line
app.use(express.methodOverride());