[英]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.