使用POST请求获取集合?

时间:2022-03-20 15:32:22

I have managed to work with REST API's to fetch() data where the urls contain minimal parameters (and use GET).

我已经成功地使用REST API来获取()数据,其中url包含最小的参数(并使用GET)。

How would one retrieve a collection through a POST request?

如何通过POST请求检索集合?

4 个解决方案

#1


63  

Also note that fetch supports Jquery.ajax parameters, so you can easily set type = post in the call.

还要注意,fetch支持Jquery。ajax参数,因此可以在调用中轻松设置type = post。

Messages.fetch({data: {api_key: 'secretkey'}, type: 'POST'});

For more parameters: http://api.jquery.com/jQuery.ajax/

更多的参数:http://api.jquery.com/jQuery.ajax/

#2


2  

You may need to extend the Collection object to install your own convention for fetches. In doing so, you would likely provide your own fetch function. Something like:

您可能需要扩展集合对象来安装自己的获取约定。这样做,您可能会提供自己的fetch函数。喜欢的东西:

fetch : function(options) {
  options || (options = {});
  var model = this;
  var success = function(resp) {
    if (!model.set(model.parse(resp), options)) return false;
    if (options.success) options.success(model, resp);
  };
  var error = wrapError(options.error, model, options);
  (this.sync || Backbone.sync)('create', this, success, error);
  return this;
}

where it uses a 'create' instead of a 'read'. On first blush, this is what I'd try first, though there may be a more elegant way to do it.

它使用的是“create”而不是“read”。乍一看,这是我首先尝试的,尽管可能有一种更优雅的方法。

The downside of this approach is that you essentially have framework code in your app and if the framework changes you might encounter problems. You would do well to compartmentalize this change into a separate layer to make it easy to update with new framework releases.

这种方法的缺点是,在应用程序中本质上有框架代码,如果框架发生变化,您可能会遇到问题。您可以很好地将此更改划分为单独的层,以便使用新的框架版本进行更新。

#3


2  

Backbone.sync is the function used to interact with the server via your models. You can provide your own implementation that issues a POST request for the 'read' method instead of GET. See http://documentcloud.github.com/backbone/#Sync

骨干。sync是通过模型与服务器交互的函数。您可以提供您自己的实现,为“read”方法而不是GET发出POST请求。看到http://documentcloud.github.com/backbone/同步

#4


1  

try {
    // THIS for POST+JSON
    options.contentType = 'application/json';
    options.type = 'POST';
    options.data = JSON.stringify(options.data);

    // OR THIS for GET+URL-encoded
    //options.data = $.param(_.clone(options.data));

    console.log('.fetch options = ', options);
    collection.fetch(options);
} catch (excp) {
    alert(excp);
}

#1


63  

Also note that fetch supports Jquery.ajax parameters, so you can easily set type = post in the call.

还要注意,fetch支持Jquery。ajax参数,因此可以在调用中轻松设置type = post。

Messages.fetch({data: {api_key: 'secretkey'}, type: 'POST'});

For more parameters: http://api.jquery.com/jQuery.ajax/

更多的参数:http://api.jquery.com/jQuery.ajax/

#2


2  

You may need to extend the Collection object to install your own convention for fetches. In doing so, you would likely provide your own fetch function. Something like:

您可能需要扩展集合对象来安装自己的获取约定。这样做,您可能会提供自己的fetch函数。喜欢的东西:

fetch : function(options) {
  options || (options = {});
  var model = this;
  var success = function(resp) {
    if (!model.set(model.parse(resp), options)) return false;
    if (options.success) options.success(model, resp);
  };
  var error = wrapError(options.error, model, options);
  (this.sync || Backbone.sync)('create', this, success, error);
  return this;
}

where it uses a 'create' instead of a 'read'. On first blush, this is what I'd try first, though there may be a more elegant way to do it.

它使用的是“create”而不是“read”。乍一看,这是我首先尝试的,尽管可能有一种更优雅的方法。

The downside of this approach is that you essentially have framework code in your app and if the framework changes you might encounter problems. You would do well to compartmentalize this change into a separate layer to make it easy to update with new framework releases.

这种方法的缺点是,在应用程序中本质上有框架代码,如果框架发生变化,您可能会遇到问题。您可以很好地将此更改划分为单独的层,以便使用新的框架版本进行更新。

#3


2  

Backbone.sync is the function used to interact with the server via your models. You can provide your own implementation that issues a POST request for the 'read' method instead of GET. See http://documentcloud.github.com/backbone/#Sync

骨干。sync是通过模型与服务器交互的函数。您可以提供您自己的实现,为“read”方法而不是GET发出POST请求。看到http://documentcloud.github.com/backbone/同步

#4


1  

try {
    // THIS for POST+JSON
    options.contentType = 'application/json';
    options.type = 'POST';
    options.data = JSON.stringify(options.data);

    // OR THIS for GET+URL-encoded
    //options.data = $.param(_.clone(options.data));

    console.log('.fetch options = ', options);
    collection.fetch(options);
} catch (excp) {
    alert(excp);
}