0

This is my JavaScript code.

function postFile(){
    var obj=new Object();
    obj.category=document.getElementsByName("gtitle")[0].value;
    obj=obj.stringify(obj);
    sendDetails("http://localhost:8080/Megabizz/webapi/gallery", obj);

    var r;

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            r=new Object(JSON.parse(ajaxRequest.responseText));
            console.log(r.status);
        }
    };
//r not accessible here
}

In the function postFile(), I have a declared a variable r now I am manipulating this r using ajaxRequest object. Now when I am trying to access this r outside the function onreadystatechange(), I am getting an error that "r is undefined".

I think that the function onreadystatechange() is declaring a new variable r instead of manipulating r declared above the onreadystatechange() function.

Tell me the way to overcome this problem.

    //Another problem

var x;
function x(){
x=document.getElementByID("upload-buton");
}
function y(){
    x.value='some text';
}

In this case, the value of x which I am setting in function y() does not remain same for the function x(). I am getting an error "cannot set property value for undefined".

Please figure out the cause behind this error.

Arpit Agrawal
  • 321
  • 1
  • 2
  • 14
  • `r` is perfectly available there; it's just not available **yet**. Why would it be? The ajax call hasn't finished yet. If you want to do something with `r`, do it inside the ajax callback. PS: you might want to change the title of your question to something like "How to access variable coming back from ajax before the ajax call is finished". –  Sep 20 '15 at 13:36
  • 3
    possible duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) –  Sep 20 '15 at 13:37
  • 1
    What you are doing here is the equivalent of ordering a pizza and then trying to eat it the moment you hang up the phone. It's not there yet. – JLRishe Sep 20 '15 at 14:01
  • hey, I edited the question check the part which is commented as "//another problem" in the last. – Arpit Agrawal Sep 21 '15 at 11:59
  • @ArpitAgrawal If you have another question, please ask another question. Don't tack on a new one here. – JLRishe Sep 21 '15 at 15:53

1 Answers1

0

Ajax (Asynchronous JavaScript and XML) makes your code Asynchronous. That means that not everything in your code happens in a straightforward manner (the code might not be executed in the order it is written).

For basic understanding async code see a tutorial Event-Based Programming: What Async Has Over Sync.

ajaxRequest.onreadystatechanges fires on state change event. After that, you are comparing ajaxRequest.readyState, which means that however r variable is always accessible, it will be changed only on 4: request finished and response is ready and will only be available after the completion of the Ajax request. Javascript will not wait for the Ajax request to finish.

A solution strongly depends on further actions you wish to do with r variable.

once
  • 131
  • 5