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.