簡體   English   中英

為什么嘗試使用我的ajax調用返回的promise時我沒有任何數據

[英]Why am I not getting any data when trying to use the promise returned from my ajax call

我已經嘗試過該論壇和其他論壇提供的幾種解決方案,但似乎無濟於事。 我不確定這是否是邏輯問題,並且無法以我想要的方式完成,或者我只是無法以正確的方式訪問返回值。

我想做的是列出一組鏈接(代碼在外部文件Main.js中),單擊鏈接后,請獲取ID並使用它進行第二次Ajax調用並打開還將打開第二個窗口(代碼位於外部文件Cites.js中),其中包含有關所單擊鏈接的更多信息。

問題是我無法從Main.js中的第二個ajax調用中獲取ID以用於我的Cites.js文件。 我試圖返回第二個ajax調用,然后使用.done()來獲取返回的數據或嘗試獲取全局變量projectId。

這是我的Main.js中的代碼

var projectId = '';
var promise = $.ajax({
    type: 'GET',
    url: 'https://www.sciencebase.gov/catalog/items?parentId=504108e5e4b07a90c5ec62d4&max=60&offset=0&format=jsonp',
    jsonpCallback: 'getSBJSON',
    contentType: "application/json",
    dataType: 'jsonp'
    }).then(function(json) {
                 var linkBase = "http://www.sciencebase.gov/catalog/item/";
                 var link = "";                     
                 var itemId = "";                    
                 var urlId = "";
                 var itemLinkLength = "";

                 $.each(json.items, function(i,item) {
                     link = linkBase + this.id;

                     $('#sbItems').append('<li><b><a href="' + link + '" id="idNum' + i + ' ">' + this.title + '</a> - </b>' + this.summary + '</li>');                     
                 });                     

                 $('#sbItems a').on('click', function (e) {
                     e.preventDefault();                         

                     var str = $(this).attr('id');                         

                     if (str.length == 7) {                      
                          itemId = str.slice(5,6);
                     } else if (str.length == 8) {
                          itemId = str.slice(5,7);
                     }

                     urlId = json.items[itemId].id;
                     //alert(urlId);

                     return $.ajax({
                                    type: 'GET',
                                    url: 'https://www.sciencebase.gov/catalog/itemLink/' + urlId + '?format=jsonp',
                                    jsonpCallback: 'getSBJSON',
                                    contentType: "application/json",
                                   dataType: 'jsonp',
                                   success: function(json) {
                                            if (json.length > 0) {
                                                projectId = json.id;
                                                window.open('Citations.html', '_self');
                                            } else {
                                                var page = linkBase + urlId;
                                                window.open(page);
                                            }

                                   },
                                   error: function(e) {
                                       console.log(e.message);
                                   }                     
                     }); // END 2nd ajax
                }); // END Click event
}); // END promise

這就是我在Cites.js中調用它的方式

promise.done(function () {
    alert(projectId);
});

謝謝您的幫助

您確實promise = $.ajax(…).then(function(){…}) -但是then方法確實為匿名函數的結果創建了一個新的保證(在jQuery的舊版本中這是pipe )-另請參見pipe()和then()文檔與jQuery 1.8中的現實

但是,由於您沒有從anon函數返回任何內容,因此您不會在promise.done()收到任何內容。

如果要執行ajax-> click-> ajax-> done鏈,則需要為點擊處理程序手動構建一個Promise。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM