在ajax()方法jQuery中未收到错误消息 [英] Not getting error message in ajax() method jquery

查看:136
本文介绍了在ajax()方法jQuery中未收到错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设:
1).我的登录表单有两个字段注册,一个按钮的密码称为login(submit)
2).我的后端脚本login.php很好,没有错误
这是我的代码,用于通过登录表单发送数据并将其与后端进行比较,现在的问题是当我提供登录所需的正确密码和注册时,该事件成功完成,但是当我提供了错误的凭据时,它没有显示错误消息如ajax()error:function()中所示.虽然当我双击登录按钮然后页面自动刷新时消息会显示一会儿.当提供错误的注册密码时,有人可以帮助我获得错误消息吗?

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;
}); 

});

推荐答案

总是执行Success函数,请尝试以下实现:

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 "";
?>

这篇关于在ajax()方法jQuery中未收到错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆