简体   繁体   English

为什么尝试使用我的ajax调用返回的promise时我没有任何数据

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

I've tried several solutions from this forum and others but nothing seems to work. 我已经尝试过该论坛和其他论坛提供的几种解决方案,但似乎无济于事。 I'm not sure if this is a logic issue and simply can not be done the way I'd like or if I'm just not accessing the return the right way. 我不确定这是否是逻辑问题,并且无法以我想要的方式完成,或者我只是无法以正确的方式访问返回值。

What I'm trying to do is list a set of links (code is in an external file Main.js) and when the link is clicked take the id and use it to make a 2nd ajax call and open will also open a 2nd window (code is in external file Cites.js) with more information about the link clicked. 我想做的是列出一组链接(代码在外部文件Main.js中),单击链接后,请获取ID并使用它进行第二次Ajax调用并打开还将打开第二个窗口(代码位于外部文件Cites.js中),其中包含有关所单击链接的更多信息。

The problem is I can't get the id from the second ajax call in Main.js to be used in my Cites.js file. 问题是我无法从Main.js中的第二个ajax调用中获取ID以用于我的Cites.js文件。 I'm trying to return the second ajax call and then use .done() to either get the data returned or try to get the global variable projectId. 我试图返回第二个ajax调用,然后使用.done()来获取返回的数据或尝试获取全局变量projectId。

Here's the code in my Main.js 这是我的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

and Here's how I'm calling it in my Cites.js 这就是我在Cites.js中调用它的方式

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

Thanks for the help 谢谢您的帮助

You do promise = $.ajax(…).then(function(){…}) - yet the then method does create a new promise for the result of the anonymous function (in old versions of jQuery this was pipe ) - see also pipe() and then() documentation vs reality in jQuery 1.8 . 您确实promise = $.ajax(…).then(function(){…}) -但是then方法确实为匿名函数的结果创建了一个新的保证(在jQuery的旧版本中这是pipe )-另请参见pipe()和then()文档与jQuery 1.8中的现实

However, since you don't return anything from the anon function you won't receive anything in promise.done() . 但是,由于您没有从anon函数返回任何内容,因此您不会在promise.done()收到任何内容。

If you want to do a chain ajax->click->ajax->done, you will need to manually build a promise for the click handler. 如果要执行ajax-> click-> ajax-> done链,则需要为点击处理程序手动构建一个Promise。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试访问通过Ajax返回的json数据中的值时我变得未定义 - Why am i getting undefined while trying to access the value in json data returned via ajax 当我尝试调用我的方法时,为什么我会变得不确定? - Why am I getting undefined when I am trying to call my method? 尝试使用Ajax访问页面时,为什么会出现Access-Control-Allow-Origin错误? - Why am I getting Access-Control-Allow-Origin error when trying to use ajax to access a page? 尝试从Electron应用程序调用Google Drive Api时,为什么会收到“超出未验证使用的每日限制”错误? - Why I am receiving 'Daily Limit for Unauthenticated Use Exceeded' error when trying to call Google Drive Api from my Electron app? 为什么我从涉及使用 Azure API 的 JavaScript 异步操作的 Promise.all 调用中得到“未定义”和空数组 []? - Why am I getting 'undefined' and empty array[] from a Promise.all call involving JavaScript async operations that use Azure APIs? 尝试从父页面在iFrame中调用函数时,为什么会收到TypeError? - Why am I getting a TypeError when trying to call a function in an iFrame from the parent page? 为什么在尝试运行我的代码时会收到 TypeError? - Why am I getting a TypeError when trying to run my code? 尝试通过Chrome扩展js脚本使用MediaWiki API时,为什么会得到空值? - Why am I getting null value when trying to use the mediawiki api from a chrome extension js script? 为什么我应该从我的Axios调用中返回“未定义”,什么时候应该返回对象? - Why am I getting back “undefined” from my Axios call, when it should return me objects? 使用从 API 调用返回的数据 Promise.then() function - Use data returned from API call outside of Promise.then() function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM