力取骨干总是使用POST [英] Force Backbone fetch to always use POST

查看:78
本文介绍了力取骨干总是使用POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要一些POST数据到其URL以获得其所需的数据集合。回答这两个问题,<一个href=\"http://stackoverflow.com/questions/6428823/backbone-js-fetch-a-collection-using-post\">Backbone.js使用后取一个集合?和<一个href=\"http://stackoverflow.com/questions/10427086/overriding-fetch-method-in-backbone-model\">Overriding在骨干模型 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, Backbone.js fetch a collection using post? 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'}));
}

,但萤火虫仍然显示了我一个404错误,因为GET正在对有问题的URL执行正在发生(和底层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?

推荐答案

复读的问题后,这里有一个方法来逼取使用 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 对所有呼叫。

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

这篇关于力取骨干总是使用POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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