getJSON 返回数据
我浏览过这里,我意识到 getJSON 看起来除了返回一个不可用的对象之外什么也没返回。我遇到的问题是我正在尝试编辑其他人的代码以从 flickr 中提取图片。我正在尝试创建一个返回图片描述的函数。我知道它不会返回信息,但是必须有一种方法来更新全局变量或以某种方式将我需要的信息传递到另一个变量中以返回到他的函数。这就是我到目前为止所掌握的要点。
function add_description(n){
var img_id = String(n);
var textInfo ="";
$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){
textInfo = String(data.photo.description._content);
alert(textInfo);
return textInfo;
})
}
这是我在你更新乔治后尝试的代码。谢谢!
var testObj=$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){
textInfo = String(data.photo.description._content);
alert(textInfo);
return textInfo;
})
I have looked through here and I realize it doesn't look like getJSON returns anything but a object that is unusable. The problem I am having is that I am trying to edit someone else's code to pull picture from flickr. I am trying to make a function that will return the description of the pictures. I know that it doesn't return information however there has to be a way to update a global variable or somehow pass off the information i need into another variable to return to his function. This is the jist of what I have so far.
function add_description(n){
var img_id = String(n);
var textInfo ="";
$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){
textInfo = String(data.photo.description._content);
alert(textInfo);
return textInfo;
})
}
this is the code I tried after your updates George. Thanks!
var testObj=$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){
textInfo = String(data.photo.description._content);
alert(textInfo);
return textInfo;
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一步是将结果分配给
textInfo
变量:$.getJSON() 调用是异步的,因此您的代码
return textInfo;
可能在 $.getJSON 之前运行() 调用完成。因此,textInfo
变量仍然是一个空字符串。您将需要从 $.getJSON() 调用中调用其他代码,或者延迟执行返回,直到异步调用完成。这个答案基于此处找到的类似答案。
编辑(根据您的问题更新):
将 return 语句包含在
$.getJSON()
范围内将不会成功。您可以将结果分配给变量textInfo
,就像您所做的那样,该值将在 $.getJSON 完成时可用。但是,您必须确保在尝试访问该值之前调用已完成。您可以使用 setTimeout() ,这看起来很混乱,或者您可以用 $.ajax() 代替 $.getJSON() 调用,这样您就可以使用“async = false”选项。这将强制调用在继续执行之前完成,从而允许在返回变量之前填充变量。
The first step is to assign the results to the
textInfo
variable:The $.getJSON() call is asynchronous, so your code
return textInfo;
probably runs before the $.getJSON() call completes. Therefore, thetextInfo
variable is still an empty string. You will need to call the other code from within the $.getJSON() call, or delay execution of the return until the asynchronous call completes.This answer is based on the similar answer found here.
EDIT (based on your question update):
You will not be successful including the return statement within scope of
$.getJSON()
. You can assign the result to the variabletextInfo
as you have done, and that value will be available when $.getJSON completes.However, you must ensure that the call has completed before attempting to accessing the value. You can use
setTimeout()
, which seems messy, or you can substitue $.ajax() for your $.getJSON() call so you can use the 'async = false' option. This will force the call to complete before execution continues, allowing the variable to be populated before returning it.照片不存在..
输出:
The photo doesn't exist..
Output: