只返回由$ .ajax传递的JSON数据的前20个结果? [英] Only return first 20 results of JSON data passed by $.ajax?

查看:146
本文介绍了只返回由$ .ajax传递的JSON数据的前20个结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的jquery片段

  $(document).ready(function(){
$ .ajax({
url:myjson.json,
dataType:'json',
成功:函数(json){
$ .each(json,function() {
alert(test);
});
},
error:function(){
},
});
});

结果可以返回1000的结果 - 我只想要前20个例子。如何最好的做到这一点?

解决方案

.each()有一个参数 index ,它是原始集合中的索引。你可以通过从回调中返回false来跳出迭代。



所以你可以这样做:

<$ p $($ index)
$ alert(test);
else
返回false;
});

正如其他人所说,做这个服务器端更好,但也许你没有访问服务器边码?


I have the following simple jquery snippet

        $(document).ready(function () {
        $.ajax({
             url:"myjson.json",
             dataType: 'json',
             success:function(json){
                 $.each(json, function() {
                    alert("test");
                });
             },
             error:function(){
             },
        });
    });

The result can return 1000's of results - i only want the first 20 for example. How would i best do this?

解决方案

The callback for .each() has an argument index which is the index in the original collection. You break out of the iteration by returning false from the callback.

So you would do something like:

$.each(json, function(index) {
    if(index < 20)
      alert("test");
    else
       return false;
});

As others has commented it is better to do this server side, but maybe you have no access to server side code?

这篇关于只返回由$ .ajax传递的JSON数据的前20个结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆