title: Submit form with ajax, asynchronously with a callback
body: <p>I realize SO is loaded with material on this subject, I've gone through it, but just can't seem to get this to work.</p>
  
  <p>My function takes a JSON response, parses it and returns a false on success and true on error (This is backwards on purpose).</p>
  
  <p>console.log shows I'm getting the correct response, but my function won't return (<code>submitme</code> remains false although it is changed to <code>true</code> inside the callback). Thus, the function always returns false.</p>
  
  <p><s>I'm guessing my callback operation is faulty but I'm not sure what I'm doing wrong.</s><br>
  I might be off here, but I think the callback is OK, it is just being executed after the script is executed (hence asynchronous).<br>
  But my question remains, how in this case can I have the form submit true if the boolean value is only being changed via ajax?</p>
  
  <p>Here's my snippet:</p>
  
  <pre class="lang-js prettyprint-override"><code>$(".form").submit(function(){
      error = $("#error");
      var submitme = false;
  
      function ajaxCall(url, type, data, callback) {
      $.ajax({
          url: url,
          type: type,
          data: data,
          success: function(response){
              try {
              obj = JSON.parse(response);
              text = obj.Error;
              error.html('');
              error.append(text);
              //if the json was parsed, return false
              submitform = false;
              }
              catch (error) {
                  //if an error was thrown, return true
                  submitform = true;
                  }
              callback(submitform);
          },
      });
  
      }
  
      function ajaxCallSuccess(submitform) {
          console.log(submitform); //This shows correct output!
          if (submitform == 'true'){ //Here I want to manipulate the global submitme variable so I can submit the form on "true"
              submitme = true; //&lt;-- this (I think) changes the global variable asynchronousy (so after the function is read), hence the outer function always returns false
          }
  
      };
  
      ajaxCall(url, type, data, ajaxCallSuccess);
  
  
      if (submitme == true) {
          return true;
      }
      else {
          return false;
      }
  });
  </code></pre>
  
  <p><strong>EDIT:</strong><br>
  I edited the post to perhaps clarify the question a little bit</p>