最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to limitspecify the json response when using jQuery.ajax - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question edited Oct 20, 2012 at 3:32 xdazz 161k38 gold badges253 silver badges278 bronze badges asked Oct 20, 2012 at 3:27 Mike BondsMike Bonds 1,04010 silver badges16 bronze badges 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
Add a comment  | 

6 Answers 6

Reset to default 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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论