i have this code:
function foo(){
var x = $.getJSON('./someFileName.txt',function(data){});
var y = x.responseText;
console.log(y);
}
when i called it via browser console (firefox), it give me undefined for the console log result.
But, when i just copy, paste and, run the foo() function body line by line in my browser console, it gives me a result which is a string.
Can anyone enlighten me why this behaviour happens? I suppose it has something to do with the jquery call but i am not sure.
Thanks!
You need to place the second and third lines of your function inside the callback like this:
function foo(){
$.getJSON('./someFileName.txt',function(data){
var y = data.responseText;
console.log(y);
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.