title: jQuery Ajax How do callbacks work?
body: <p>Hello fellow programmers! I just started an additional programming project and swore to god my code will bo SO much cleaner and easily upgradeable than it has been before.</p>
  
  <p>Then I stumbled upon my "arch enemy" the jQuery AJAX returning. Last time I wanted to return something from an AJAX call I had to bend over and just make the call synchronous. That made things sticky and ugly and I hope that this time I will find something better.</p>
  
  <p>So I have been googling/searching stackoverflow for a while now, and just don't understand this solution many ppl has gotten which is called <em>callback function</em>. Could someone give me an example on how I could exploit these <em>callback functions</em> in order to return my login statuses:</p>
  
  <pre><code>function doLogin(username, password) {
      $.ajax({
          url:        'jose.php?do=login&amp;user='+username+'&amp;pass='+password,
          dataType:   'json',
          success:    function(data) {
                          if(data.success==1) {
                              return('1');
                          } else {
                              return('2');
                          }
                          $('#spinner').hide();
                      },
          statusCode: {
                          403:function() {
                              LogStatus('Slavefile error: Forbidden. Aborting.');
                              $('#spinner').hide();
                              return (3);
                          },
                          404:function() {
                              LogStatus('Slavefile was not found. Aborting.');
                              $('#spinner').hide();
                              return (3);
                          },
                          500:function() {
                              LogStatus('Slavefile error: Internal server error. Aborting.');
                              $('#spinner').hide();
                              return (3);
                          },
                          501:function() {
                              LogStatus('Slavefile error: Not implemented. Aborting.');
                              $('#spinner').hide();
                              return (3);
                          }
                      },
          async:      true
      });
  }
  </code></pre>
  
  <p>So as you probably know, you cannot use <code>return</code> the way I have done from inside an AJAX call. You should instead use <em>callback functions</em> which I have no idea of how to use.</p>
  
  <p>I'd be VERY greatful if someone could write me this code using <em>callback functions</em> and explaining to me just HOW they WORK.</p>
  
  <p>EDIT:</p>
  
  <p>I REALLY need to return stuff, not use it right away. This function is being called from within another function and should be able to be called from different places without being rewritten even slightly.</p>
  
  <p>/EDIT</p>
  
  <p>Sincerly,</p>
  
  <p>Akke</p>
  
  <p>Web Developer at Oy Aimo Latvala Ab</p>