我一定在做一个基本的javascript错误。我的脚本无法传递一个变量,我找不到原因。这是代码和评论。无法传递变量
var earth;
function loadWorld(world) {
// loads the world from a Json file using php
$.ajax("loadfile.php", {
dataType: "json",
async: false
}).done(function (result) {
console.log(result);
//Here consul.log provides the expected results, confirming that the file has been loaded into the result.
world = result;
console.log(world);
//Here too, properly transferred into world.
});
};
function button1() {
loadWorld(earth);
console.log(earth);
//But here console.log tells me earth is undefined!
showData(earth);
}
我已经使用稀土作为全局变量尝试和简单地分配给它的功能loadworld(),像这样:
earth = result;
我还使用返回功能尝试。但是一旦我离开荷载世界,地球总是不确定的。任何想法为什么?
can yoiu show a jsFIddle? – Konza
http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Satpal
什么是“地球”应该包含?在将它作为函数参数传递之前,您需要给它一个值。 – Barmar