7

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!

1
  • Don't you want to add a parameter (e.g. /people?5) to limit the output in the server side?
    – r_31415
    Commented Oct 20, 2012 at 3:34

6 Answers 6

10

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.

1
  • Thanks a lot for your answer. I am developing a small rails app, so I will definitely be doing the filter server side. I am also using rabl. I believe it should be no problem.
    – Mike Bonds
    Commented Oct 20, 2012 at 3:54
3

Yes, you would use the 'data' argument to pass a parameter back to your server indicating which records you want returned. I typically do this with pagination to get rows 1-10, or 21-30. This requires your server logic to understand that from the parameter values it needs to return the correct amount of data back. If you didn't have control of that (server always sent you the 100 records) then in your success handler you would manually pull out the 3 records you wanted.

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

you would have to limt the result on the server side depending on your response type. If the response is in JSON you could make a for loop at make it stop at the 3rd results. I would personnaly go for the server-side since i will reduce the response size.

2

If you're opting to do this client-side:

The first argument to the success callback is the data returned from the server.

Since the type of data that you're expecting back from the server is JSON, a JavaScript object will be returned. You would access the first or last 3 people as you would normally do in JavaScript.

For example if response from the server is in the form of the following:

{ 
    "people" : [
        { name: "Foo" },
        { name: "Bar" },
        { name: "Baz" },
        // and so on...
    ]
} 

You could access the first or last 3 people like so:

$.ajax({
   type: "GET",
   url: "/people"
   dataType: "json",
   success: function(data) {
      // Assuming there are 100 people in the "people" array
      // The first three people 
      console.log( data.people[0] ); // "Foo"
      console.log( data.people[1] ); // "Bar"
      console.log( data.people[2] ); // "Baz"

   } 
});
1
  • Thanks for your help, but I will definitely be doing any filtering on the server. I thought about taking the approach you posted before I posted here, and thought better of it.
    – Mike Bonds
    Commented Oct 20, 2012 at 3:57
1

If I understand fine.....

I usually send data in the ajax request. In your case I'd send this:

 url:'addres'
 data: 'from='+value_from+'&to='+to;
 type:'post'

In the server side you can get from and to, or something like that (amount if you want, or another option), and response with the results you want

0

I totally agree with @xdazz answer, but if you make use of external resources and do not have access to the server side script, then you can go for this approach, which worked a treat for my solution. In your for loop add this:

for (var i = 0; i < 5 && i < response.data; i++) {

//Response is limited to five. 
//example: 
var items = response[i].data.items

};

It is a bit of a legacy approach, but is works.

Not the answer you're looking for? Browse other questions tagged or ask your own question.