Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch a collection using a POST request?

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

How would one retrieve a collection through a POST request?

like image 689
mike Avatar asked Jun 21 '11 16:06

mike


People also ask

Can you fetch data from POST request?

You can get more data from the request, refer to the documentation. POST request using fetch API: The post request is widely used to submit forms to the server. Fetch also supports the POST method call.

How does fetch POST work?

The fetch() method: Fetch API comes with a fetch () method that allows you to fetch data from all sorts of different places and work with the data fetched. It allows you to make an HTTP request, i.e., either a GET request (for getting data) or POST request (for posting data).

Is fetch a GET request?

Fetch defaults to GET requests, but you can use all other types of requests, change the headers, and send data.

How do I transfer data using Fetch?

You tell fetch() to go to a URL by passing it an argument, e.g. fetch("https://flatironschool.com") , and it makes a network request. You chain calls to fetch() with then() . Each then() call takes a callback function as its argument.


3 Answers

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

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

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

like image 124
Arvid Janson Avatar answered Oct 06 '22 23:10

Arvid Janson


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);
}
like image 43
Walter von Entferndt Avatar answered Oct 07 '22 00:10

Walter von Entferndt


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 : 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.

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.

like image 2
Bill Eisenhauer Avatar answered Oct 06 '22 23:10

Bill Eisenhauer