jquery - How to check on ajax success when the server response is true or false? -


i have server response true or false. have been trying figure out how check on success if returned value true or false following script :

$('form[data-async]').on('submit', function(event) { var $form = $(this); var $target = $($form.attr('data-target'));  $("input#login").val('connexion...');  $.ajax({     type: $form.attr('method'),     url: $form.attr('action'),     data: $form.serialize(),     cache: false,      success: function(data) {         if (data == true) {             alert('success :  user logged in');         } else {             alert('erreur login');         }     },     error: function(){         alert("failure");     } });  event.preventdefault(); }); 

i have been trying possible value data data == 'true' & data == 1 still not working. idea please ? here called function :

        function login ($username, $password){         if (!isset($_session['vivvo'])){             $_session['vivvo'] = array();         }         if (isset($_session['vivvo']['login_fail_time']) && ($_session['vivvo']['login_fail_time'] < (vivvo_start_time - 60*60))){             $_session['vivvo']['login_fail'] = 0;             $_session['vivvo']['login_fail_time'] = 0;         }          if ($_session['vivvo']['login_fail'] >= 3){             $this->set_error_code(2751);             return false;         }          $user_list = $this->get_user_list(null);         $user = $user_list->get_user_data($username, $password);         if ($user == false){             $_session['vivvo']['login_fail'] = $_session['vivvo']['login_fail'] + 1;             $_session['vivvo']['login_fail_time'] = time();             if ($_session['vivvo']['login_fail'] >= 3){                 $this->set_error_code(2751);             }elseif ($_session['vivvo']['login_fail'] >= 2){                 $this->set_error_code(2752);             }else{                 $this->set_error_code(2753);             }             return false;         }else{             if ($user->get_activated() == '1'){                 $sm =& vivvo_lite_site::get_instance();                 $sm->user = $user;                 $sm->user->set_privileges();                 if (!isset($_session['vivvo'])){                     $_session['vivvo'] = array();                 }                 $_session['vivvo']['user_id'] = $sm->user->get_id();                 $_session['vivvo']['user_domain'] = vivvo_user_source;                 $_session['vivvo']['login_fail'] = 0;                 $_session['vivvo']['login_fail_time'] = 0;                 session_regenerate_id();                 return true;             }else{                 $this->set_error_code(2754);                 return false;             }         }     } 

you missing datatype attribute in ajax request. include it

$.ajax({     type: $form.attr('method'),     url: $form.attr('action'),     data: $form.serialize(),     datatype : 'html', //set datatype attribute     cache: false,      success: function(data) {         console.log(data); //try log data , check response         if (data == true) {             alert('success :  user logged in');         } else {             alert('erreur login');         }     },     error: function(){         alert("failure");     } }); 

then try console.log(data) inside success function see recieving server.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -