强制 Backbone fetch 始终使用 POST [英] Force Backbone fetch to always use POST

查看:35
本文介绍了强制 Backbone fetch 始终使用 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要将一些数据发布到其 url 以获取所需数据的集合.这两个问题的答案,使用POST请求获取集合?在主干模型中覆盖 fetch() 方法,让它看起来像我应该能够让它像这样工作:

I have a Collection that needs to POST some data to its url to get the data it needs. The answer to these two questions, Fetch a collection using a POST request? and Overriding fetch() method in backbone model, make it seem like I should be able to get it to work like this:

fetch: function( options ) {
  this.constructor.__super__.fetch.apply(this, _.extend(options,{data: {whatever: 42}, type: 'POST'}));
}

,但 Firebug 仍然向我显示 404 错误,这是因为正在针对相关 url 执行 GET(并且底层 Rails 路由只允许 POST).这应该有效吗?如果是这样,我还可以尝试什么?如果没有,我做错了什么?

, but Firebug still shows me a 404 error that is happening because a GET is being executed against the url in question (and the underlying Rails route only allows POST). Should this be working? If so, what else might I try? If not, what have I done wrong?

推荐答案

再次阅读问题后,这里有一种方法可以强制 fetch 在每次 fetch 调用时使用 POST.(感谢评论)

After reading the question again, here's a way to force the fetch to use POST per fetch call. (Thanks for the comments)

yourCollection.fetch({
    data: $.param({id: 1234}), 
    type: 'POST', 
    success: function(d){
        console.log('success');
    }
});

另一种方法是覆盖 AJAX 实现本身以对所有调用使用 POST.

Another approach is to override the AJAX implementation itself to use POST for all calls.

Backbone.ajax = function() {
    var args = Array.prototype.slice.call(arguments, 0);
    _.extend(args[0], { type: 'POST' });
    return Backbone.$.ajax.apply(Backbone.$, args);
};

这篇关于强制 Backbone fetch 始终使用 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆