[英]getJSON returning data
I have looked through here and I realize it doesn't look like getJSON returns anything but a object that is unusable.我查看了这里,我意识到它看起来不像 getJSON 返回任何东西,而是一个不可用的 object。 The problem I am having is that I am trying to edit someone else's code to pull picture from flickr.我遇到的问题是我正在尝试编辑其他人的代码以从 flickr 中提取图片。 I am trying to make a function that will return the description of the pictures.我正在尝试制作一个 function ,它将返回图片的描述。 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.我知道它不会返回信息,但是必须有一种方法来更新全局变量或以某种方式将我需要的信息传递给另一个变量以返回他的 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.这是我在您更新 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;
})
The first step is to assign the results to the textInfo
variable:第一步是将结果分配给textInfo
变量:
$.getJSON(...) {
textInfo = String(data.photo.description._content);
The $.getJSON() call is asynchronous, so your code return textInfo;
$.getJSON() 调用是异步的,因此您的代码return textInfo;
probably runs before the $.getJSON() call completes.可能在 $.getJSON() 调用完成之前运行。 Therefore, the textInfo
variable is still an empty string.因此, textInfo
变量仍然是一个空字符串。 You will need to call the other code from within the $.getJSON() call, or delay execution of the return until the asynchronous call completes.您将需要从 $.getJSON() 调用中调用其他代码,或者延迟执行返回,直到异步调用完成。
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()
.在$.getJSON()
的 scope 中包含 return 语句将不会成功。 You can assign the result to the variable textInfo
as you have done, and that value will be available when $.getJSON completes.您可以将结果分配给变量textInfo
,当 $.getJSON 完成时,该值将可用。
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.你可以使用setTimeout()
,这看起来很乱,或者你可以用 $.ajax() 代替你的 $.getJSON() 调用,这样你就可以使用 'async = false' 选项。 This will force the call to complete before execution continues, allowing the variable to be populated before returning it.这将强制调用在执行继续之前完成,允许在返回之前填充变量。
The photo doesn't exist..照片不存在。。
console.log(data);
Output: Output:
code: 1
message: "Photo "2121" not found (invalid ID)"
stat: "fail"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.