Ajax 猫鼬快捷酒店;主干-主干模型.remove()不工作
我正在开发一个节点应用程序,使用Express、Mongoose和带有木偶的主干网 除删除路由外,所有路由都工作正常 如果我调用this.model.destroy,总是会出现以下错误:Ajax 猫鼬快捷酒店;主干-主干模型.remove()不工作,ajax,node.js,backbone.js,express,mongoose,Ajax,Node.js,Backbone.js,Express,Mongoose,我正在开发一个节点应用程序,使用Express、Mongoose和带有木偶的主干网 除删除路由外,所有路由都工作正常 如果我调用this.model.destroy,总是会出现以下错误: DELETE http://localhost:3000/api/user 404 (Not Found) DELETE http://localhost:3000/api/user 404 (Not Found) 404是在Express的删除路径中返回的,就像Express不支持404一样,但我在网
DELETE http://localhost:3000/api/user 404 (Not Found)
DELETE http://localhost:3000/api/user 404 (Not Found)
404是在Express的删除路径中返回的,就像Express不支持404一样,但我在网上看到了许多使用404的例子
以下是我的设置:
猫鼬模式:
var UserSchema = new mongoose.Schema(
{
name: String,
email: String,
age: Number
});
User = mongoose.model('User', UserSchema);
ExpressJS路线:(不工作) 或 主干模型.destroy()调用此路由,但返回错误404
ExpressJS user.js控制器:(工作正常,但由于之前的404问题而无法联系到)
BackboneJS模型
var User = Backbone.Model.extend({
idAttribute: "_id",
url: '/api/user/',
});
BackboneJS客户端视图
var UserView = Backbone.Marionette.ItemView.extend(
{
template: Handlebars.compile($('#userView').html()),
events:
{
'click .delete-button': 'deleteUser'
},
deleteUser: function(event)
{
this.model.remove();
}
});
我总是会遇到这样的错误:
DELETE http://localhost:3000/api/user 404 (Not Found)
DELETE http://localhost:3000/api/user 404 (Not Found)
但是,如果我使用此直接ajax调用,它会起作用:
jQuery.ajax({
url:'/api/user/' + this.model.id,
type: 'DELETE',
success:function(data, textStatus, jqXHR)
{
}
});
那么,如果我通过Ajax调用路由,而主干网内部也使用Ajax,那么这为什么会起作用呢?为什么主干网无法生成如此简单的模型。destroy()
有没有一种方法可以像上面的Ajax示例那样配置主干Model.destroy方法?谢谢找到了问题。主干模型.remove()未发送id,因为我以以下方式使用“url”:
Backbone.Model.extend({
url: '/users',
//...
});
这将告诉主干网使用/users作为所有操作的URL
要确保使用“url”发送id,可以使用以下函数:
url: function() {
return '/list_items/' + encodeURIComponent(this.id)
}
或者更好地使用“urlRoot”而不是“url”,让默认的“url”函数添加id:
urlRoot: '/users'
使用urlRoot就像一个符咒一样工作执行删除请求时,似乎没有设置模型的id。HTTP DELETE请求url不应该是,例如,其中35是用户的id吗?看起来用户的id没有被发送……是的,urlRoot更简单。如果使用urlRoot,则不需要将url定义为函数。将url定义为函数更好地保留给使用查询参数的更复杂的url。
urlRoot: '/users'