jQuery ajax函数错误

时间:2014-03-13 12:37:18

标签: php jquery ajax

我有一个Ajax请求的以下jquery函数:

function ajax_submit(){
    var submit_val= $('#login_form').serialize();
    dest = "login.php";
    $.ajax({
        type:"POST",
        url: dest,
        data: submit_val,
        success: function(data){
            if(data=='ok'){
                $("#spnmsg").fadeTo(200,0.1,function(){
                                $(this).removeClass().addClass('spn_success').html("Everything ok, logging you in...").fadeTo(900,1,function(){
                location.href="index.php";
                });
                });
            }else if(data=='ko'){
                $("#spnmsg").fadeTo(200,0.1,function(){
                $(this).removeClass().addClass('spn_error').html("Wrong Username or password. Please, try again").fadeTo(900,1);
                $("#txtUsername").focus();
                });
            }else if(data=='problemi'){
                $("#spnmsg").fadeTo(200,0.1,function(){
                $(this).removeClass().addClass('spn_error').html("Could not connect. Maybe system is down for maintenance. Please, try again later.").fadeTo(900,1);
                $("#txtUsername").focus();
                });
            }
        }
    });
};

问题在于,即使login.php返回ok,ko或problemi,并且变量数据得到正确的值,它也不会在if语句中循环。到目前为止我做错了什么? 谁能找到我的错误? 非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试使用trim()

如果(data.trim()==' OK'){