Reputation: 1040
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!
Upvotes: 7
Views: 9438
Reputation: 354
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.
Upvotes: 0
Reputation: 2563
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"
}
});
Upvotes: 2
Reputation: 399
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
Upvotes: 1
Reputation: 1078
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.
}
});
Upvotes: 3
Reputation: 160833
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
.
Upvotes: 10
Reputation: 487
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.
Upvotes: 2