0
我试图一次提出两个或多个请求,如果这甚至可能?我很关心速度,因为在第一次请求完成后,我想将该信息显示在网页上,然后对每个附加网址执行相同的操作。是否有可能使用延缓对象使用Jsonp进行多个调用?
我一直在阅读有关延迟对象,并尝试一些例子,到目前为止,我已经试过这样做,
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script >
$(document).ready(function($) {
// - 1st link in chain - var url = 'https://www.sciencebase.gov/
catalog/items?parentId=504108e5e4b07a90c5ec62d4&max=60&offset=0&format=jsonp';
// - 2nd link in chain - var url = 'https://www.sciencebase.gov/
catalog/itemLink/504216b6e4b04b508bfd333b?format=jsonp&max=10';
// - 3rd (and last) link in chain - var url = 'https://www.sciencebase.gov/
catalog/item/4f4e4b19e4b07f02db6a7f04?format=jsonp';
// parentId url
function parentId() {
//var url = 'https://www.sciencebase.gov/catalog/items?parentId=
504108e5e4b07a90c5ec62d4&max=3&offset=0&format=jsonp';
return $.ajax({
type: 'GET',
url: 'https://www.sciencebase.gov/catalog/items?parentId=
504108e5e4b07a90c5ec62d4&max=3&offset=0&format=jsonp',
jsonpCallback: 'getSBJSON',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {},
error: function(e) {
console.log(e.message);
}
});
}
// itemLink url
function itemLink() {
//var url = 'https://www.sciencebase.gov/catalog/itemLink
/504216b6e4b04b508bfd333b?format=jsonp&max=10';
return $.ajax({
type: 'GET',
url: 'https://www.sciencebase.gov/catalog/itemLink
/504216b6e4b04b508bfd333b?format=jsonp&max=10',
jsonpCallback: 'getSBJSON',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {},
error: function(e) {
console.log(e.message);
}
});
}
// Multiple Ajax Requests
$.when(parentId(), itemLink()).done(function(parentId_data, itemLink_data) {
console.log("parentId_data.items[0].title");
});
});
但它似乎并不像功能正常。我期待能够在函数内部的.when()方法后面添加一些内容,以告诉我的程序该做什么,但是我没有显示任何内容?
感谢您的帮助!
感谢帮助测试工作的例子。我仍然对这部分感到困惑,“传递给回调函数的参数是每个请求的参数数组”。另外,我原来的问题可能没有意义。我想要做的是从第一个链接获取信息并显示在网页上。我知道如果我使用ajax请求的第一个网址,并把这样一个for循环像这样 – jc72
- 从上面的评论data.items [i],继续id,以获得id显示。一旦我开始使用.when()像你所拥有的一样,我不能得到任何显示,也不会给我任何错误?我应该做parent_data.items [i] .id? – jc72
得到它的工作,抱歉迟到的答复,谢谢! – jc72