title: Simple jquery ajax return
body: <p>i have this simple script:</p>
  
  <pre><code>function paginateUsers(page){
     get( _config_remote_host+'/users?page='+page,function(json){
  
        json = JSON.parse(json);
          var _html = "";
          var json_users;
          var json_num_users;
  
          if(json.users){
          json_users = json.users;
          json_num_users = json.number_of_users;
          }
          if(!json.users){
              json_users = json;
              json_num_users = 0;
          }
          for(i = 0; i &lt; json_users.length; i ++){
            if(i == 0){ _html += '&lt;div class="row-fluid separator20"&gt;';}
            _html += '&lt;div class="span3 media userbox"&gt;'
                        +'&lt;div class="row-fluid"&gt;'
                         + '&lt;div class="span3"&gt;'
                          + ' &lt;a class="thumbnail" href="#"&gt;'
                           +' &lt;img src="jon.png" alt=""/&gt;'
                          +'  &lt;/a&gt;'
                         +' &lt;/div&gt;'
                          +'&lt;div class="span9"&gt;'
                           +' &lt;ul class="unstyled"&gt;'
                            +'  &lt;li&gt;&lt;strong&gt;&lt;a href="user.html?id='+json_users[i].id+'"&gt;'+json_users[i].first_name+'&lt;/strong&gt;&lt;/br&gt;'+json_users[i].last_name+'&lt;/a&gt;&lt;/li&gt;'
  
                               +'   &lt;li&gt;&lt;h6&gt;New york city&lt;/h6&gt;&lt;/li&gt;'
                          +'    &lt;li&gt;&lt;a class="pull-right btn btn-mini"&gt;Contact&lt;/a&gt;&lt;/li&gt;'
                         +'   &lt;/ul&gt;'
                        +'  &lt;/div&gt;'
                       +' &lt;/div&gt;'
                      +'&lt;/div&gt;';
                  if(i%3 == 2){ _html += '&lt;/div&gt;&lt;div class="row-fluid separator20"&gt;';}
               } 
  
          $('.users-list').html(_html).hide().fadeIn(100);
          //return always total users number to calculate total links number
          return json_num_users;
          });
  
    }
  </code></pre>
  
  <p>then i do</p>
  
  <pre><code>var n = paginateUsers(1);
      alert(n)
  </code></pre>
  
  <p>but it alerts always 'undefined' , what's the real problem here?</p>
  
  <p>here the ajax call and init functs:</p>
  
  <pre><code>function createCORSRequest(_method, _url){
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
      // Check if the XMLHttpRequest object has a "withCredentials" property.
      // "withCredentials" only exists on XMLHTTPRequest2 objects.
      xhr.open(_method, _url, true);
    } else if (typeof XDomainRequest != "undefined") {
      // Otherwise, check if XDomainRequest.
      // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
      xhr = new XDomainRequest();
      xhr.open(_method, _url);
    } else {
      // Otherwise, CORS is not supported by the browser.
      xhr = null;
    }
    return xhr;
  }
  
  /*END INIT FUNCTION*/
  
  function get(_url, _callback){
    var xhr = createCORSRequest('GET',_url);
    if (!xhr){
      throw new Error('CORS not supported');
    }
    $('.ajax-loading').show();
    xhr.send();
  
    /*SUCCESS -- do somenthing with data*/
    xhr.onload = function(){
      // process the response.
      _callback(xhr.responseText);
      $('.ajax-loading').hide();
    };
  
    xhr.onerror = function(e){
      console.log(e);
      $('.ajax-loading').show();
    };
  }
  </code></pre>
  
  <p>thanks</p>