1

I want to working with login system jQuery ajax and json. I'm facing no problem with success function. I dn't know how to through custom errors like "incorrect login".

Here is my code, Please help me.

<script>
    $(document).ready(function(){
        $("#login").click(function(){
            if($("#defaultEmail").val() == ""){
                $("#defaultEmail").addClass("field-err");
                $("#defaultEmail").focus();
            }else if($("#defaultPassword").val() == ""){
                $("#defaultEmail").removeClass("field-err");
                $("#defaultPassword").addClass("field-err");
                $("#defaultPassword").focus();
            }else{
                $("#defaultPassword").removeClass("field-err");
                $('#loading').css('display', 'block');
                var email = $('#defaultEmail').val();
                var password = $('#defaultPassword').val();
                var queryString = "email=" + email + "&password=" + password + "";
                $.ajax({
                  url: 'secureLogin.php',
                  contentType: "application/json; charset=utf-8",
                  dataType:'json',
                  async: false,
                  type:'GET',
                  data: queryString,
                  success: function(data){
                    $('#loading').css('display', 'none');
                    var url = "c_profile.php";
                    $(location).attr('href',url);
                  },
                  error: function(request,error){
                    $('#loading').css('display', 'block');
                    $("#log-ajax-error").append("ERROR: " + error);
                  }
                });
            }
        });
    });
</script>

Here is my sucureLogin.php code

$email = $_GET['email'];
$password = $_GET['password'];
$encrypted_pass = md5($password);

    $sel = @mysql_query("SELECT `name`, `company`, `cid` FROM `company` WHERE `email` = '$email' AND `password` = '$encrypted_pass'");
    $count = @mysql_num_rows($sel);
    if($count > 0){
        while($data = @mysql_fetch_array($sel)){
                       // Success here
        }
    }else{
        $arr = array('a' => 'Unable to login');
        echo json_encode($arr);
    }

i'm throwing json_encode for the error. Please help me with this. Thanks in advance.

4

2 回答 2

3

在服务器端

$arr = array('error' => 'Unable to login');
echo json_encode($arr);

在客户端

success: function(data){
    $('#loading').hide(); 
    if( 'error' in data ){ 
        $("#log-ajax-error").append("ERROR: " + data.error);
    }
    else{
       var url = "c_profile.php";
       $(location).attr('href',url);
    }
},

如果 ajax 请求出错,将调用错误回调,因此您的代码需要在成功处理程序中。

于 2012-10-01T13:36:52.110 回答
1

在ajax中,错误处理程序不构成你抛出的错误,它构成了连接错误,或者是请求的错误。这意味着,无论您如何发回错误响应,它都会触发成功回调。

在您成功后,您应该对响应进行一些检查:

成功:函数(数据){

   如果(数据.状态 === 假)
   {

      // 有一个错误

   } 别的 {

      $('#loading').css('display', 'none');
      var url = "c_profile.php";
      $(location).attr('href',url);

   }

},

并在您的 PHP 处理程序中添加一个状态键:

$arr = 数组(
   '状态' => 真(或假),
   'a' => '无法登录'
);
于 2012-10-01T13:36:26.923 回答