Not getting error message in ajax() method jquery

2019-09-01 02:53发布

Assume:
1).my login form have two field enrollment,password with one button called login(submit)
2). My backend script login.php is fine no error there
here is my code for sending data via login form and comparing it to backend,now the problem is when i provide right password and enrollment which are needed for login,the event successfully completed but when i provide the wrong credentials its not showing an error message as displayed in ajax() error:function().although the message is displayed for a while when i double click the login button and then page automatically gets refreshed.can anyone help me out to getting error message when wrong password is provided for enrollment

$(function(){
/*$('#err').hide();*/
$('.err').hide();
/*$('.error').hide();*/
$('#eno').blur(function(){
    var eno = $('input#eno').val();
    if(eno == ""){
        $('#err_eno').show();
        $('input#eno').focus();
        return false;
    }else{
        $('#err_eno').hide();
    }   
});

$('#pw').blur(function(){
    var pw = $('input#pw').val();
    if(pw == ""){
        $('#err_pw').show();
        $('input#pw').focus();
        return false;
    }else{
        $('#err_pw').hide();
    }
});

$('#submit_btn').click(function(){

    var dataString = $('#log').serialize();
//alert (dataString);return false;  
$.ajax({
            type:"POST",
            url:"script/login.php",
            data:dataString,
            success:function(){
                    window.location='profile.php';
            },
            error:function(){
                $('#err').html("<p>Provide right combination</p>");
            }
        });
return false;
}); 

});

2条回答
趁早两清
2楼-- · 2019-09-01 03:22

The error function in called when a request fails, In your case the ajax function is executed successfully. What you have to do is set the ajax function's dataType property to json and pass a json array as response from server. See more info here

Example below:

$.ajax({
            type:"POST",
            url:"script/login.php",
            data:dataString,
            dataType:"json",
            success:function(data){
                    if(data.error==true){     
                      $('#err').html("<p>Provide right combination</p>");
                    }else{
                        window.location='profile.php';
                    }
            },
            error:function(){

            }
        });

});

Your login.php

//if success 
 echo json_encode(array('success'=>TRUE));
//else if password failed
echo json_encode(array('error'=>TRUE));

I hope this is helpful.

查看更多
狗以群分
3楼-- · 2019-09-01 03:36

Well your Success function is always executed, Try with the following implementation:

$.ajax({
            type:"POST",
            url:"script/login.php",
            data:dataString,
            success:function(x){
            if (x == 1)
            {
        window.location='profile.php';
            }
            else
            {
        //Handle Error Here
                $('#err').html("<p>Provide right combination</p>");

            }
          }  
        });

login.php

<?php

//execute code

//if success 
 print "1";
//else
print "";
?>
查看更多
登录 后发表回答