Laravel 5 Backbone.js destroy 方法上的令牌不匹配,已设置令牌
Laravel 5 TokenMismatch on Backbone.js destroy method, with _token set
我正在尝试通过以下方式删除模型:
var FeaturedUsersView = Backbone.View.extend({
events: {
"click .remove-featured-user" : "removeFeaturedUser"
},
template : _.template(FeaturedUsersTemplate),
render: function(){
this.$el.html(this.template({ featuredUsers: App.featuredUsers.toJSON() }));
return this;
},
removeFeaturedUser: function(event) {
userToRemove = this.collection.get(event.target.id);
userToRemove.set("_token", $('input[name="_token"]').val());
userToRemove.destroy({
wait: true,
/*
*headers: {
* _token : $('input[name="_token"]').val()
*},
*async: false,
*/
url: '/api/admin/versions/' + App.config.get("class_id") + '/featured_users',
});
}
});
我一直收到错误:异常 'Illuminate\Session\TokenMismatchException'。
这个相同的视图有一个使用相同标记的 model.save()。我感觉 destroy 方法忽略了 DELETE 请求中的令牌。有什么建议吗?
这里是backbone.js的销毁方法:
destroy: function(options) {
options = options ? _.clone(options) : {};
var model = this;
var success = options.success;
var wait = options.wait;
var destroy = function() {
model.stopListening();
model.trigger('destroy', model, model.collection, options);
};
options.success = function(resp) {
if (wait) destroy();
if (success) success.call(options.context, model, resp, options);
if (!model.isNew()) model.trigger('sync', model, resp, options);
};
var xhr = false;
if (this.isNew()) {
_.defer(options.success);
} else {
wrapError(this, options);
xhr = this.sync('delete', this, options);
}
if (!wait) destroy();
return xhr;
},
全局添加这个解决了我的问题:
<meta name="csrf-token" content="{{ csrf_token() }}">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
我正在尝试通过以下方式删除模型:
var FeaturedUsersView = Backbone.View.extend({
events: {
"click .remove-featured-user" : "removeFeaturedUser"
},
template : _.template(FeaturedUsersTemplate),
render: function(){
this.$el.html(this.template({ featuredUsers: App.featuredUsers.toJSON() }));
return this;
},
removeFeaturedUser: function(event) {
userToRemove = this.collection.get(event.target.id);
userToRemove.set("_token", $('input[name="_token"]').val());
userToRemove.destroy({
wait: true,
/*
*headers: {
* _token : $('input[name="_token"]').val()
*},
*async: false,
*/
url: '/api/admin/versions/' + App.config.get("class_id") + '/featured_users',
});
}
});
我一直收到错误:异常 'Illuminate\Session\TokenMismatchException'。
这个相同的视图有一个使用相同标记的 model.save()。我感觉 destroy 方法忽略了 DELETE 请求中的令牌。有什么建议吗?
这里是backbone.js的销毁方法:
destroy: function(options) {
options = options ? _.clone(options) : {};
var model = this;
var success = options.success;
var wait = options.wait;
var destroy = function() {
model.stopListening();
model.trigger('destroy', model, model.collection, options);
};
options.success = function(resp) {
if (wait) destroy();
if (success) success.call(options.context, model, resp, options);
if (!model.isNew()) model.trigger('sync', model, resp, options);
};
var xhr = false;
if (this.isNew()) {
_.defer(options.success);
} else {
wrapError(this, options);
xhr = this.sync('delete', this, options);
}
if (!wait) destroy();
return xhr;
},
全局添加这个解决了我的问题:
<meta name="csrf-token" content="{{ csrf_token() }}">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});