0

Below is a javascript function that i haved tried to return a status, but it's unable to return true false.It'just show not defined in console.log but "available" and "not available" is perfectly printed out.

function checkingeUserInput(){
    var status;
    $.ajax({
      type:'POST',
      dataType:'json',
      data: userCheck,
      url: api_url,
      success:function(resp){
        var cond = resp.items;
        if(cond.valid==true){
          status = true;
          console.log('available');
        }
        if(cond.valid==false){
          console.log('not available');
          status = false;
        }
      }
    });
   return status;
}

And below a piece of code from click function to get the status true false

var status = checkingeUserInput();
    console.log(status); // I have got undefined here when click function is execute.
smile
  • 35
  • 5
  • there's some basic things in your code that make me cringe -- please read a good book on programming. – Marcus Müller Jul 10 '15 at 22:18
  • because ajax requests are asyncronous, you will always be unable to get the "status" value, the fastest you can do is create a callback function and invoke it in the success ajax callback, so that it will be executed when the ajax request is **completed**. Moreover, if you search here in stackoverflow, you will find many questions about that – briosheje Jul 10 '15 at 22:19

0 Answers0