如何从函数内部获取 JavaScript 变量值?
我正在使用 JQuery getJSON 函数来获取一些 JSON 格式的数据。
这些数据将与 YouTube JavaScript API 一起使用:http://code.google。 com/apis/youtube/js_api_reference.html
JSON 数据不是从 YouTube 提取的。这是一个简单的手写 YouTube 视频 ID 和标题列表,并以 CSV 形式列出。我正在使用 PHP 读取 CSV 并创建 JSON 对象。
问题的根源在于,当我将任何 YouTube JavaScript API 代码放入“document.ready”中时,YouTube 代码会神秘地停止工作。
我已经让完整的播放器正常运行(HTML 页面上包含 JSON),但现在当我尝试外部化数据(使用 $getJSON)时,我遇到了这个障碍。为了让我的播放器正常工作,我需要首先将 JQuery 代码放在 document.ready 中。接下来,我需要 YouTube 播放器代码,它应该位于 document.ready 之外。
这是我获取 $getJSON 返回的数据的失败尝试:
$(document).ready(function(){
$.getJSON('bar.php', function(data) {
var baz = data;
return baz;
}); // getJSON
}) // ready
console.log("baz: " + baz);
如何获取 baz 的值?
I'm using the JQuery getJSON function to get some JSON-formatted data.
The data will be used along with the YouTube JavaScript API: http://code.google.com/apis/youtube/js_api_reference.html
The JSON data is not pulled from YouTube. It's a simple, hand-written list of YouTube Video ID's and Titles which are listed on a CSV. I'm using PHP to read the CSV and create a JSON object.
The root of the problem is that when I put any of the YouTube JavaScript API code within "document.ready", the YouTube code mysteriously ceases to work.
I have gotten the complete player to function (with JSON inclded on the HTML page), but now I am facing this obstacle as I try to externalize the data (use $getJSON). For my player to work, I need to have the JQuery code first and inside document.ready. Next, I need to have the YouTube player code and it should be outside of document.ready.
Here is my failed attempt to get the data returned by $getJSON:
$(document).ready(function(){
$.getJSON('bar.php', function(data) {
var baz = data;
return baz;
}); // getJSON
}) // ready
console.log("baz: " + baz);
How to I get the value of baz?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,您不能这样做,原因有两个。
第一个可以解决的问题是,变量的范围是构成 getJSON 方法第二个参数的匿名函数。
第二个更难处理的是异步 JavaScript 和 XML 是异步。
getJSON
方法设置下载正在进行,但脚本随后继续运行。console.log
行在 baz 被赋值之前到达。任何用 JavaScript 编写的复杂程序都是事件驱动的。当您设置某些内容(例如 Ajax 调用)时,您必须通过回调方法处理其结果。
在传递给 getJSON 的匿名函数(或从那里调用的另一个函数)中执行任何需要执行的操作。
In this case, you can't, for two reasons.
The first, which could be worked around, is that the scope of the variable is the anonymous function that forms the second argument to the
getJSON
method.The second, which is rather harder to deal with, is that Asynchronous JavaScript and XML is Asynchronous. The
getJSON
method sets a download going, but the script then continues running. Theconsole.log
line is reached beforebaz
has been assigned a value.Any complex program written in JavaScript will be event driven. When you set something in motion (such as an Ajax call), you have to handle the results of that via the callback method.
Do whatever needs to be done in the anonymous function you pass to
getJSON
(or in another function called from there).为什么不能在
getJSON
函数中对返回的数据执行您想要的操作?Why can't you do what you want with the returned data within the
getJSON
function?jQuery.getJSON
是 AJAX 调用的简写:因此您必须使用回调中的数据,或任何需要从回调中调用该数据的代码。
jQuery.getJSON
is a shorthand for the AJAX call:So you will have to work with the data within the callback, or any code that requires that data to be called from within the callback.