2012-11-20 38 views
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()方法后面添加一些内容,以告诉我的程序该做什么,但是我没有显示任何内容?

感谢您的帮助!

回答

1

问题的一部分是,在$ .when的完成处理程序中,传递给回调的参数是每个请求的参数数组,而不仅仅是要使用的数据。您可以使用.pipe来解决此问题,如下例所示。

另外,除非你有一个很好的理由,否则不要指定jsonpCallback,大多数时候你想让jQuery在你的内部管理它。

这里有JSFiddle

jQuery(function($) { 

    function parentId() { 
     return $.ajax({ 
      url: 'https://www.sciencebase.gov/catalog/items?parentId=504108e5e4b07a90c5ec62d4&max=3&offset=0&format=jsonp', 
      dataType: 'jsonp', 
      error: function(e) { 
       console.log(e.message); 
      } 
     // We'll use pipe here so that rather than the value being passed to our $.when handler 
     // is simply our data rather than an array in the form of [ data, statusText, jqXHR ] 
     }).pipe(function(data, statusText, jqXHR) { 
      return data; 
     }); 
    } 

    function itemLink() { 
     return $.ajax({ 
      url: 'https://www.sciencebase.gov/catalog/itemLink/504216b6e4b04b508bfd333b?format=jsonp&max=10', 
      dataType: 'jsonp', 
      error: function(e) { 
       console.log(e.message); 
      } 
     }).pipe(function(data) { 
      return data; 
     }); 
    } 

    // Multiple Ajax Requests 
    $.when(parentId(), itemLink()).done(function(parentId_data, itemLink_data) { 
     console.log(parentId_data, itemLink_data); 
    }); 
}); 
+0

感谢帮助测试工作的例子。我仍然对这部分感到困惑,“传递给回调函数的参数是每个请求的参数数组”。另外,我原来的问题可能没有意义。我想要做的是从第一个链接获取信息并显示在网页上。我知道如果我使用ajax请求的第一个网址,并把这样一个for循环像这样 – jc72

+0

- 从上面的评论data.items [i],继续id,以获得id显示。一旦我开始使用.when()像你所拥有的一样,我不能得到任何显示,也不会给我任何错误?我应该做parent_data.items [i] .id? – jc72

+0

得到它的工作,抱歉迟到的答复,谢谢! – jc72

相关问题