ajaxComplete -表单提交/验证问题


ajaxComplete - form submission / validation issue

我有一个PHP页面与登录表单。当单击表单时,执行后端验证,json返回登录验证的状态。

点击表单的提交按钮时执行的代码:

// When DOM is ready
$(document).ready(function(){       
    // When the form is submitted
    $("#status > li > form").submit(function(){
        // Hide 'Submit' Button
        $('#submit').hide();
        // Clear the error messages if any
        $('#error_response').html("");
        // 'this' refers to the current submitted form 
        var str = $(this).serialize();
        var ran_no = new Date().getTime();                  
        str += "&ran="+ran_no;
        // -- Start AJAX Call --
        $.ajax({ 
                type: "POST", 
                url: "process.php",  // Send the login info to this page
                data: str, 
                dataType:'json',
                success: function(msg){
                    $(document).ajaxComplete(function(event, request, settings){ 
                    // Hide Gif Spinning Rotator
                    $('#ajax_loading').hide();
                    // Show 'Submit' Button
                    $('#submit').show();
                    if(msg.error_array == "OK") // LOGIN OK?
                    {  
                    var login_response = '<li><div class="alert alert-success" id="ajax_response"><i class="icon-spinner icon-spin pull-left"></i> Succes...</div></li>';
                    // Replace the dropdownlist contents
                    $('#status').html(login_response); // Refers to 'status'
                    // After 3 seconds redirect the 
                    setTimeout('go_to_private_page()', 3000); 
                    }  
                    else // ERROR?
                     {  
                        var login_response = "";
                        $.each(msg.error_array,function(i,element)
                        {
                        login_response = "<div class='"alert alert-error'">" + element + "<br>";
                        });
                        $('#ajax_response').html(login_response);
                     }  
                    });  
                    }  
                }); 
                // -- End AJAX Call --
        return false;
    }); // end submit event
    });

登录验证调用的php页面返回一个Json对象

function procLogin(){
    global $session, $form;    
    /* Login attempt */
    // database call  
    $retval = $session->login($_POST['username'], $_POST['password'], isset($_POST['remember_me']));
    $results = array ();
    /* Login successful */
    if($retval){
        $results['error_array'] = array('OK');
        echo json_encode($results);
    }
    /* Login failed */
    else{
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    $results['value_array'] = $_POST;           //Holds submitted form field values 
    $results['error_array'] = $form->getErrorArray();   //Holds submitted form error messages  
    echo json_encode($results);
    }
}

当用户第一次成功登录时,一切正常。

如果用户第一次输入了无效的密码并进行了更正,第二次输入时仍然会收到无效的密码消息。

相同的代码不使用ajax/json工作良好。

这里有什么问题吗?

可能是缓存问题。尝试添加:

cache: false

到Ajax调用