Click here to Skip to main content
15,922,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
<pre>
var json="";
var xrequest = new XMLHttpRequest();
xrequest.open('GET', 'videos.json', true);
xrequest.onload = function(){
	if(this.status == 200) {
		var json = JSON.parse(this.responseText);
		for(var i=0; i<json.length; i++) {
			console.log(json[i].url);
		}
		console.log(json + ' 1');
	}
}
xrequest.send(null);

console.log(json);


What I have tried:

this code reads from json file and the response is assigned to var json, then i wanna access the json from outside the function,

global var json="";
im supposing this var json gets assigned to json.parse object,
console logs prints that var json successfully assigned to response object json.parse,

however now im trying to manipulate it outside the xmlhttprequest object, but it returns nothing, why the json response has disappreared outside the xmlhttprequest,

how can i access the object response outside the function??? pls sos, help
Posted
Updated 30-Jan-18 7:42am

The reason you cannot access it is because inside the function it also says
var json =

which means it declares a new variable named json. Just remove the var and it will assign to the global json variable.
 
Share this answer
 
Comments
ilhomelv 26-Jan-18 15:05pm    
thank u for your answer, but i tried that as well, with no var declared inside the response object, however that did not work as well, ((

that send back undefined
ZurdoDev 26-Jan-18 17:14pm    
Then you'll have to debug it. It may be your page is posting back so the js is reloading. I don't know without being able to see it happen.
ilhomelv 27-Jan-18 1:40am    
no its not my page, i simply cant find the problem there, i tried several browsers, debugging simply showing the line of undefined code, you cant debug one line, i found the solution in different way, but however im still curios if someone can find the way to assign the value of respone object to a global variable, that be nice to know, however this taks was solved by simply adding a callback function to the end of code with json param being sent to callback function. thanks for now for no help.
ZurdoDev 27-Jan-18 16:08pm    
We do it all the time. Not sure why you couldn't get it to work. But it's something specific to what you're doing.
ilhomelv 28-Jan-18 6:55am    
i passed it inside call() and that works, but when im trying to assign it to a globally declared variable it keeps printing undefined, i did checked about hoisting, may be variable is hoisted, ((((
Quote:
xrequest.send(null);
console.log(json);

The XMLHttpRequest.send()[^] method returns immediately. It does not wait until the response has been received.

The json variable is not assigned until the response has been received. Therefore, the json variable is still undefined in your call to console.log.

You need to re-think what you're doing.
 
Share this answer
 
Comments
ilhomelv 30-Jan-18 13:53pm    
thanks, i will rethink and i got solution, simply by a callback(jsondata) this works ))
but i was thinking of assigning the var to global var soomehow may be

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900