Wordpress Ajax登录系统问题 - 302

时间:2015-07-02 06:42:16

标签: javascript jquery ajax wordpress login

我试图用Wordpress创建一个简单的Ajax登录系统。 不幸的是,每次" wp_signon"函数被触发,我的系统失败了,我唯一的信息就是这个:

POST myurl/wp-admin/admin-ajax.php - 302 Found
GET  myurl/?login = failed - 200 Found

因此,无论我是否尝试使用良好的信息登录,我的JS脚本都会出现在"错误部分"我的Ajax功能。

谁能告诉我我做错了什么?非常感谢!

非常感谢!

JS:

jQuery(document).on('submit', loginForm, function(event) {

    event.preventDefault();

    var usernameVal = jQuery('.modal-login .login-form #user_login').val();
    var passwordVal = jQuery('.modal-login .login-form #user_pass').val();
    var remembermeField = jQuery('.modal-login .login-form #rememberme');
    var securityVal = jQuery('.modal-login .login-form #security').val();

    if ( remembermeField.prop('checked') ) {
        var remembermeVal = 'true';
    } else {
        var remembermeVal = 'false';
    }

    jQuery.ajax({
        type : "post",
        url : data_front_end_users_management.ajaxurl,
        dataType: 'json',
        data: { 
            action: 'AuthUserAjax',
            username: usernameVal, 
            password: passwordVal, 
            rememberme: remembermeVal,
            security: securityVal
        },
        success: function(data) {

            if ( data['answer'] === 1 ) {

                modalAlert( msgRow, msgAlert, msgContent, false, 'alert-success', 'alert-danger', data['message'] );


            } else {

                modalAlert( msgRow, msgAlert, msgContent, false, 'alert-danger', 'alert-success', data['message'] );

            }

        //END success    
        },
        error: function () {

            modalAlert( msgRow, msgAlert, msgContent, false, 'alert-danger', 'alert-success', defaultError );

        }

    //END ajax
    });

//END jQuery(document).on('submit', loginForm, function(event)
});

行动:

function AuthUserAjax() {

    $LoginController = new FELoginController();

    $value = array();
    $value['answer'] = 0;
    $value['message'] = '';

    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $rememberme = $_POST['rememberme'];
    $security = $_POST['security'];

    if ( $LoginController->checkAuthInfosFilled( $username, $password ) ) {

        $user = $LoginController->UserAuth( $username, $password, $rememberme );

        if ( $user ) {

            if ( is_wp_error( $user ) ) {

                $value['answer'] = 0;
                $value['message'] = $user->get_error_message();

            } else {

                $value['answer'] = 1;
                $value['message'] = 'Success.';

            }

        //END if ( $user )
        } else {

            $value['answer'] = 0;
            $value['message'] = 'Wrong username or password.';

        }

    } else {

        $value['answer'] = 0;
        $value['message'] = 'Please, enter your username and your password.';

    }

    $data = json_encode($value);

    die( $data );

//END AuthUserAjax 
}

add_action( 'wp_ajax_AuthUserAjax','AuthUserAjax' );
add_action( 'wp_ajax_nopriv_AuthUserAjax','AuthUserAjax' );

CONTROLLER:

public function UserAuth( $username, $password, $rememberme ) {

    if ( check_ajax_referer( 'ajax-login-nonce', 'security' ) ) {

        $creds = array();
        $creds['user_login'] = $username;
        $creds['user_password'] = $password;

        if ( $rememberme == 'true' || $rememberme == true ) {
            $creds['remember'] = true;
        } else {
            $creds['remember'] = false;
        }

        $user = wp_signon( $creds, false );

        return $user;

    } else {

        return false;

    }

//END UserAuth
}

1 个答案:

答案 0 :(得分:0)

所以,经过几个小时的搜索,我终于找到了我的问题:我班上的另一种方法干扰了我的ajax认证系统。