Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit/specify the json response when using jQuery.ajax

So I have only recently began using ajax with jQuery. I am wondering if it is possible to limit or specify what you want back from the response.

So say I had the following, and I only wanted to get the first 3 people or the last 3 out of a 100 people.

$.ajax({
   type: "GET",
   url: "/people"
   dataType: "json",
   success: function(data) {
      // Do some awesome stuff.
   }
});

Now, I know you can pas and optional data object. Could this data object be used to limit or specify the response I want?

Thanks for any help!

like image 770
Mike Bonds Avatar asked Oct 20 '12 03:10

Mike Bonds


1 Answers

You should do the filter in the server side. Pass the parameter use data.

$.ajax({
   type: "GET",
   url: "/people",
   data: {limit: 3, order: 'desc'}, 
   dataType: "json",
   success: function(data) {
      // Do some awesome stuff.
   }
});

Then in the server side, return the response based on limit and order.

like image 77
xdazz Avatar answered Sep 23 '22 09:09

xdazz