I am making an Ajax call in a another function function checkUrl(url){...}
. When this function is called and Ajax request has been sent, and a response is got. I have declared a variable var str;
inside this function checkUrl(url){...}
. function, which is not in the scope of the Ajax function, so it means that variable str
is global to the Ajax function. What I need is, when I get the response I want to assign that response to that global variable. But due to some reasons the variable is not getting to be initialized and its return
statement is returning undefined value. When I initially assign an empty string or null value like var str = null;
or var str = " ";
then the function function checkUrl(url){...}
. returns that value which has been initialized initially. I don't know why its not getting initialized during run time when I get the response.
My Code
function checkUrl(url){
var str;
$.ajax({
method:"GET",
url:"http://www.bitbaysolutions.com/connections.php?fbPostUrl=true",
data:{
url:url
},
success:function(r){
console.log(r);
switch(r){
case "not_using_ext":{
console.log("Script2 Says After Response : User is not using the extension ! ");
str = "not_using_ext";
break;
}
case "using_ext":{
console.log("Script2 Says After Response : User is using the extension !");
str = "using_ext";
break;
}
}
}
});
alert("Value of str is : " + str);
return str;
}