How do i get access to the data return from jquery ajax call. Outside the $.ajax( ).
//Reloads the inital page
function jobexist( jobname )
{
   var dataString = 'jobname=' + jobname;
   var found = false;
   $.ajax(
   {
      type: 'POST',
      url: "/genode/jobs/jobexist.m",
      data: dataString,
      dataType: 'json',
      success: function(data)
      {
         alert("Passed");
         if( data.result == 0 )
         {
            found = true;
          }else{  
            found = false; 
          }
      },
      error: function( data )
      {
         alert("Login Failed");
         return -1; //alert(data);
      }
   });
   if( found == true )
   {
       alert("found!")'
       return true;
   }else{
       alert("not found!");
       return false;
    }
}
if( !jobexist(jobname) )
{
    $("#jobname_error").text("This jobname already exist.");
    $("#jobname_error").show();
    return false;
}
Ajax works asynchronously so your if found statement will be hit before the ajax call finishes.
What you can do is call a function from inside your ajax success function and pass whatever data you want to it
function found(data){
   if( data.result == 0 )
   {
       alert("found!")
       return true;
   }else{  
       alert("not found!");
       return false;
   }
}
$.ajax(
   {
      type: 'POST',
      url: "/genode/jobs/jobexist.m",
      data: dataString,
      dataType: 'json',
      success: function(data)
      {
         alert("Passed");
         found(data);
      },
      error: function( data )
      {
         alert("Login Failed");
         return -1; //alert(data);
      }
   });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With