0

I am trying to return the Response of a $.ajax query using the while loop which runs into an infinite loop Heres the code

//The function that makes the jax call and stores the response
function findResponse(Reqtype,Requrl) {
var response;
while(!response) {
    function keepFinding() {
        $.ajax({
            type: Reqtype,
            url: Requrl,
            success:function(data) {
                response=data;
            }
        })
    }
}
return response;}

And here is the Call to the function

var emailResponse = findResponse("get","form.php?emailField="+x);
        console.log(emailResponse);
  • Variable scoping here... http://stackoverflow.com/questions/4146144/variable-scope-in-ajax-calls – briosheje Mar 09 '15 at 15:41
  • Just do: while (!window.response) & window.response = data. This is still very ugly and I'd not use it. The problem you have is because of JS scoping. Basically you're not setting the outer response-variable in your callback. Check @briosheje comment for solving the issue without relying on manipulating the window object. – posixpascal Mar 09 '15 at 15:43

0 Answers0