关注"开发Backbone js" http://addyosmani.github.io/backbone-fundamentals/#talking-to-the-server(搜索"解析函数")
单击"删除":即使使用此"解析"也不会从服务器(dbase)中删除该书。函数操作正常...给出了DELETE http命令,并给出了正确的书籍ID ...但是这并没有从dbase中删除它...
生成的URL命令如下所示:
DELETE http://localhost:4711/api/books/5417ff846b205d9c05000001
...这是在server.js
中触发以下功能app.delete( '/api/books/:id', function( request, response ) {
console.log( 'Deleting book with id: ' + request.params.id );
...
...但DELETE命令永远不会"返回" (在FF控制台中,你只需得到旋转器,它不会消失)......
答案 0 :(得分:1)
在server.js中,按如下方式设置服务器:
// Module dependencies.
var application_root = __dirname,
express = require("express"), // Web framework
path = require("path"), // Utilities for dealing with file paths
mongoose = require('mongoose'); // MongoDB integration
//Create server
var app = express.createServer();
// Configure server
app.configure(function () {
app.use(express.bodyParser()); // parses request body and populates req.body
app.use(express.methodOverride()); // checks req.body for HTTP method overrides
app.use(app.router); // perform route lookup based on url and HTTP method
app.use(express.static(path.join(application_root, "public"))); // Where to serve static content
app.use(express.errorHandler({ dumpExceptions:true, showStack:true })); // Show all errors in development
});
//Start server
app.listen(4711, function () {
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
// Connect to database
mongoose.connect('mongodb://localhost/library_database');
//Schemas
var Book = new mongoose.Schema({
title:String,
author:String,
releaseDate: Date
});
//Models
var BookModel = mongoose.model('Book', Book);
尝试按如下方式创建删除路线:
app.delete('/api/books/:id', function(req, res){
console.log('Deleting book with id: ' + req.params.id);
return BookModel.findById(req.params.id, function(err, book){
return book.remove(function(err){
if(!err){
console.log('Book removed');
return res.send('');
} else {
console.log(err);
}
});
});
});
通过AJAX进行测试:
$.ajax({
url:'/api/books/5417ff846b205d9c05000001',
type: 'DELETE',
success:function(data, textStatus, jqXHR){
console.log("Post resposne:");
console.dir(data);
console.log(textStatus);
console.dir(jqXHR);
}
});