0

I am using an AJAX request to check if something is true or false. Here is the full function including the AJAX request:

function selectAnswer(id, questionId) {
    $.get("php/forms/select-answer-process.php?scope=get&id="+questionId, function(data) { if (data == "true") { alert("You can only select one answer per question"); } });

    var confirmChoice = confirm("Are you sure you want to select this as the correct answer? This cannot be undone.");
    if (confirmChoice) {
        $.get("php/forms/select-answer-process.php?scope=save&id="+id);
        document.getElementById("answer-check-"+id).src = "img/icons/check-green.png";
    } else {
        return false;
    }
}

The alert works great, but I want to exit the parent function if the AJAX response is true. How can I do that?

Arun
  • 626
  • 10
  • 29
  • 1
    Your `ajax` call is asynchronous and as a result you cannot base anything past the async call on the returned value from ajax. You should use a callback. – Travis J May 30 '13 at 20:48
  • 2
    You have to move all the logic inside the Ajax callback. If you require to return anything from the parent function, you have to make it accept a callback or use promises. See this question for more information: http://stackoverflow.com/q/14220321/218196. – Felix Kling May 30 '13 at 20:49

3 Answers3

2

As a result of ajax being asynchronous, anything after the ajax call will still execute after the ajax call is made. Instead, a callback is commonly used after the ajax returns in order to use the returned data. You should use the data returned from the ajax call in a callback like this:

function selectAnswer(id, questionId) {
 $.get("php/forms/select-answer-process.php?scope=get&id="+questionId, function(data) {
  if (data == "true") { 
   alert("You can only select one answer per question"); 
  }else{
   successResponse(id);//callback when ajax is complete
  }
 });
}

//function to be called if ajax completion is successful and correct
function successResponse(id){
 var confirmChoice = confirm("Are you sure you want to select this as the correct answer? This cannot be undone.");
 if (confirmChoice) {
    $.get("php/forms/select-answer-process.php?scope=save&id="+id);
    document.getElementById("answer-check-"+id).src = "img/icons/check-green.png";
 }
}
Travis J
  • 81,153
  • 41
  • 202
  • 273
0

You can throw an exception from inside the block and catch it in the parent, then handle it as needed:

try {
  get block... {
    if (data == "true") { 
      alert("You can only select one answer per question"); 
      throw "nope!";
    }
  }
}
catch(ex) {
  return false;
}
Matt
  • 13,948
  • 6
  • 44
  • 68
0

Try this:

function selectAnswer(id, questionId) {
    $.get("php/forms/select-answer-process.php?scope=get&id="+questionId, function(data) {
        if (data == "true") {
            alert("You can only select one answer per question");
            return;
        }
        var confirmChoice = confirm("Are you sure you want to select this as the correct answer? This cannot be undone.");
        if (confirmChoice) {
            $.get("php/forms/select-answer-process.php?scope=save&id="+id);
            document.getElementById("answer-check-"+id).src = "img/icons/check-green.png";
        } else {
            return false;
        }
    });
}
wachme
  • 2,327
  • 20
  • 18