javascript - jQuery ajax not firing in child function -


i'm making script pretty large i'm taking object oriented javascript approach. when trying use jquery's ajax (or before shorthand document ready) doesn't fired , have no clue why.

(copy-pasting whole document ready code chrome console shows ajax call work, doensn't run inside child function)

my code:

// main class function arina(loginstate) {     var loginstate = loginstate; }   /**  * attempts log user in  * @  * @param  {string} username [user's username]  * @param  {string} password [user's password]  * @return {array}           [status, message]        */ arina.prototype.login = function(username, password) {     /**      * validate if user has filled in required fields      */     if (username == '' || username == null)  return [false, 'no username given.'];      if (password == '' || password == null)  return [false, 'no password given.'];      /**      * set ajax payload      */     var payload = {         username : username,         password : password,         unhashed  : true     }       /**      * document ready jquery      */     $(document).ready(function(){             /**          * make ajax call check credentials          * @param {object} 'payload' [form data]          */         $.ajax({             url     : '/arina/app',             type    : 'post',             data    : payload         }).always(function(response, status, xhr){             if (response == 'success') {                 return [true, 'login successful.'];             } else {                 return [response, status, xhr];             }              console.log("i'm not getting fired!!"); // code ever reaches this.         });            /**          * catch jquery ajax error          * @param  {object} event                 * @param  {object} xhr                   * @param  {object} settings              * @param  {string} thrownerror           * @return {array}                       */         $(document).ajaxerror(function(event, xhr, settings, thrownerror) {             if (xhr.status == 401) return [false, 'invalid username or password.'];             if (xhr.status == 500) return [false, 'internal server error, please contact administrator.'];         });     }); }  // call method once script has been loaded (along rest of js files) var arina = new arina(1); arina.login('test', 'test'); 

update

the child function fire ajax, never calls callback function.

console.log("i'm not getting fired!!"); not reached because return statements above it.

here working example

two major changes:

  • callback function of $.ajax corrected
  • $(document).ready removed

additional info:
instead of:

    $.ajax({         url     : '/arina/app',         type    : 'post',         data    : payload     }) 

you use:

    $.post('/arina/app', data) 

instead of always check on result use:

    .done(function() {         // handle success     })     .fail(function() {         // handle fail     }) 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -