0

Hello i use this piece of code to convert json file to an object. My problem is that i can't use the variable respons in any other function. Anybody knows how to solve this problem?

var response;

function AJAX_JSON_Req( url )
{
    var AJAX_req = new XMLHttpRequest();
    AJAX_req.open( "GET", url, true );
    AJAX_req.setRequestHeader("Content-type", "application/json");

    AJAX_req.onreadystatechange = function()
    {
        if( AJAX_req.readyState == 4 && AJAX_req.status == 200 )
        {
            response = JSON.parse( AJAX_req.responseText );        
        }
    }
    AJAX_req.send();
}
Willem Dewilde
  • 121
  • 1
  • 9
  • Is this a typo? `respons` and later `response`. – emerson.marini Aug 07 '14 at 12:03
  • 2
    As you don't show the other code where you try to access `response` it is not possible to tell. Probably you try to access `response` before the `onreadystatechange` callback is called. – t.niese Aug 07 '14 at 12:06
  • It was a typo on this site but not in my local code – Willem Dewilde Aug 07 '14 at 12:07
  • 1
    the question is *when* do you use your variable? because if you use it before `onreadystatechange` is triggered, then it would still be `undefined`, since it's asynchronous. – Antoine Aug 07 '14 at 12:07

3 Answers3

0

response is set asynchronously. It isn't populated until the AJAX response arrives. You won't be able to access it until that happens. Any code that depends on that variable should be inside your onreadystatechange callback.

var response;

function AJAX_JSON_Req( url )
{
    var AJAX_req = new XMLHttpRequest();
    AJAX_req.open( "GET", url, true );
    AJAX_req.setRequestHeader("Content-type", "application/json");

    AJAX_req.onreadystatechange = function()
    {
        if( AJAX_req.readyState == 4 && AJAX_req.status == 200 )
        {
            response = JSON.parse( AJAX_req.responseText );    

            // Code that depends on response should be in this scope.
        }
    }
    AJAX_req.send();
}

AJAX_JSON_Req('http://something')
response // undefined here
joews
  • 29,767
  • 10
  • 79
  • 91
-1

I was facing this problem too. Then i have taken an input type hidden field in the dom, store ajax response or result there, when required I have pulled the result data from the hidden field. here you also need asynchronous false.

Arka
  • 581
  • 5
  • 18
-2
AJAX_req.open( "GET", url, true );

last parameter means async true, if you want to define parameter in this function and use this in other functions, you can't do this (I mean this depends on response time). you can use false for last parameter maybe helps you. but this time you will wait response for continue

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Engin Üstün
  • 1,118
  • 11
  • 18
  • The cases where you use a sync request are really rare. For normal usage you never would like to use a sync request. `[...] if u want to define parameter in this function [...]` the definition (`var response;`) is outside of the function, only the place where the variable is set is inside of the function. And that result for sure can be used in other functions, as long as `response` is in the same _scope_, onyl the time _when_ you try to access the variable is important when the call is async. – t.niese Aug 07 '14 at 12:14