简体   繁体   中英

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!

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 .

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.
 }
});

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.

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"

   } 
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM