1
function getMensages(){
var xhr = new XMLHttpRequest();
xhr.open('GET', '../mensagens.php');
xhr.send();
xhr.onload=function(){
if (xhr.readyState==4 && xhr.status==200){
jsonText = xhr.responseText;
jsonObj = JSON.parse(jsonText);
}
return jsonObj;
}
}
getMensages();
console.log(jsonObj);
How do I get the variable jsonObj
outside the scope of the function xhr.onload()
and out of function getMensages()
? When calling the function xhr.onload()
is already returning object I want but need the object in the variable outside the scopes.
http://answall.com/questions/26767/fazer-um-post-com-ajax-e-json-com-javascript-puro?rq=1
– bfavaretto
You can put the die into a global variable:
window.jsonObj = jsonObj;
– CesarMiguel
Good morning Kadu, maybe your question seems to be about callbacks, maybe this answer will help you: http://answall.com/a/45721/3635
– Guilherme Nascimento