Javascript AJAX只加载和返回有限数量的对象
我刚刚开始使用AJAX,我想知道是否可以指定从AJAX调用返回的对象数量 为了进行测试,我从创建了一个JSON.db文件 我的代码如下所示(使用Polymer web组件): HTML:Javascript AJAX只加载和返回有限数量的对象,javascript,ajax,count,return-value,Javascript,Ajax,Count,Return Value,我刚刚开始使用AJAX,我想知道是否可以指定从AJAX调用返回的对象数量 为了进行测试,我从创建了一个JSON.db文件 我的代码如下所示(使用Polymer web组件): HTML: <body> <h1>Object-list</h1> <template is="dom-bind"> <evo-object_list> </evo-object_list> </template>
<body>
<h1>Object-list</h1>
<template is="dom-bind">
<evo-object_list>
</evo-object_list>
</template>
<h1 id = 'loadInHere'></h1>
<button onclick = 'loadDoc()'>Load</button>
<script>
function loadDoc(){
var element = document.querySelector('evo-object_list');
element.loadDoc();
}
</script>
</body>
JSON
{
"fathers" : [
{
"id" : 0,
"married" : false,
"name" : "Ronald Taylor",
"sons" : null,
"daughters" : [
{
"age" : 9,
"name" : "Barbara"
}
]
},
{
"id" : 1,
"married" : false,
"name" : "Kevin Lopez",
"sons" : null,
"daughters" : [
{
"age" : 6,
"name" : "Angela"
}
]
},
{
"id" : 2,
"married" : true,
"name" : "Matthew Harris",
"sons" : null,
"daughters" : [
{
"age" : 19,
"name" : "Helen"
}
]
},
{
"id" : 3,
"married" : true,
"name" : "Thomas Lewis",
"sons" : null,
"daughters" : [
{
"age" : 16,
"name" : "Michelle"
},
{
"age" : 8,
"name" : "Dorothy"
}
]
},
{
"id" : 4,
"married" : true,
"name" : "John Martin",
"sons" : null,
"daughters" : [
]
}
]
}
etc.....
我的文件可能很长,我不想加载整个内容,是否可以定义文件中的哪些对象以及应该返回多少。e、 g只选择前三位父亲,然后在重新按下按钮后选择下三位父亲
我现在只使用JSON进行测试,但可能还有其他我现在不知道的文件
请仅提供纯javascript答案我不想使用jQuery,正如其他人所说,这主要是一个应该在服务器端实现的功能,但为了给您提供一些指导,我们的想法是从JS端发送一个参数,例如:
// Note the limit parameter sent to the server
xhttp.open("GET", "db.json?limit=5", true);
// Note the page parameter sent to the server
xhttp.open("GET", "db.json?page=1", true);
// You can even set custom paginations!
xhttp.open("GET", "db.json?page=5&per_page=50", true);
此实现的一个示例是堆栈溢出问题列表,请注意URL中的参数
这应该是服务器端的一项功能(例如,当您调用支持分页的API时),但由于您获取的是静态内容,所以只能限制显示为HTML的内容。您可以从
JS
告诉服务器设置内容限制。除此之外,我认为这在客户端是不可能的。您的应用程序基本上是使用javascript来访问ajax端点(基本上是一个Web服务),因此Web服务应该支持分页。否则,您将一无所获,因为使用纯javascript,您所能做的就是每次下载所有内容并转储您不需要的内容
// Note the limit parameter sent to the server
xhttp.open("GET", "db.json?limit=5", true);
// Note the page parameter sent to the server
xhttp.open("GET", "db.json?page=1", true);
// You can even set custom paginations!
xhttp.open("GET", "db.json?page=5&per_page=50", true);