如何在使用jQuery.ajax时限制/指定json响应

时间:2022-10-07 17:43:36

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.

所以我最近才开始在jQuery中使用ajax。我想知道是否可以限制或指定您想要从响应中返回的内容。

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.

所以说我有以下内容,我只想得到前三个人或100人中的最后三个人。

$.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?

现在,我知道你可以使用pas和可选的数据对象。这个数据对象可以用来限制或指定我想要的响应吗?

Thanks for any help!

谢谢你的帮助!

5 个解决方案

#1


9  

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.

然后在服务器端,根据限制和顺序返回响应。

#2


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.

是的,您可以使用'data'参数将参数传递回服务器,指示您想要返回哪些记录。我通常使用分页来获取行1-10或21-30。这要求您的服务器逻辑理解从所需的参数值返回正确数量的数据。如果您没有控制权(服务器总是向您发送100条记录),那么在您的成功处理程序中,您将手动提取所需的3条记录。

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

#3


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.

您必须根据您的响应类型限制服务器端的结果。如果响应是JSON,你可以进行for循环,使其停在第3个结果。我个人会去服务器端,因为我会减少响应大小。

#4


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.

由于您期望从服务器返回的数据类型是JSON,因此将返回JavaScript对象。您可以像在JavaScript中一样访问第一个或最后一个3个人。

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:

您可以访问第一个或最后3个人,如下所示:

$.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"

   } 
});

#5


1  

If I understand fine.....

如果我明白了.....

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

我通常在ajax请求中发送数据。在你的情况下,我发送这个:

 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

在服务器端,你可以从中获取,或者类似的东西(如果你想要的数量,或其他选项),并回复你想要的结果

#1


9  

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.

然后在服务器端,根据限制和顺序返回响应。

#2


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.

是的,您可以使用'data'参数将参数传递回服务器,指示您想要返回哪些记录。我通常使用分页来获取行1-10或21-30。这要求您的服务器逻辑理解从所需的参数值返回正确数量的数据。如果您没有控制权(服务器总是向您发送100条记录),那么在您的成功处理程序中,您将手动提取所需的3条记录。

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

#3


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.

您必须根据您的响应类型限制服务器端的结果。如果响应是JSON,你可以进行for循环,使其停在第3个结果。我个人会去服务器端,因为我会减少响应大小。

#4


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.

由于您期望从服务器返回的数据类型是JSON,因此将返回JavaScript对象。您可以像在JavaScript中一样访问第一个或最后一个3个人。

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:

您可以访问第一个或最后3个人,如下所示:

$.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"

   } 
});

#5


1  

If I understand fine.....

如果我明白了.....

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

我通常在ajax请求中发送数据。在你的情况下,我发送这个:

 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

在服务器端,你可以从中获取,或者类似的东西(如果你想要的数量,或其他选项),并回复你想要的结果