jQuery AJAX调用适用于localhost,不适用于实时服务器

时间:2014-09-03 12:44:51

标签: javascript php jquery ajax wordpress

我有一个我目前无法解决的问题。 我在这个例子中的网站上有一个WordPress AJAX登录: http://natko.com/wordpress-ajax-login-without-a-plugin-the-right-way

在我的localhost上一切正常,可以登录。在实时服务器上,我无法登录,响应始终为0

在我的login.php中,我有这段代码:

function cted_ajax_login_init() {

wp_register_script( 'ajax-login-js', get_template_directory_uri() . '/js/ajax-login.js', array('jquery') );
wp_enqueue_script( 'ajax-login-js' );

wp_localize_script( 'ajax-login-js', 'ajax_login_object', array(
    'ajaxurl'           => admin_url( 'admin-ajax.php' ),
    'redirecturl'       => $_SERVER['REQUEST_URI'],
    'loadingmessage'    => __('Gegevens verifiëren, een ogenblik ...')
));

// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxlogin', 'cted_ajax_login' );
add_action( 'wp_ajax_ajaxlogin', 'cted_ajax_login' );
}

if ( !is_user_logged_in() ) {
add_action( 'init', 'cted_ajax_login_init' );
}

ajax功能:

function cted_ajax_login(){

// First check the nonce, if it fails the function will break
check_ajax_referer( 'ajax-login-nonce', 'security' );

// Nonce is checked, get the POST data and sign user on
$info = array();
$info['user_login'] = $_POST['username'];
$info['user_password'] = $_POST['password'];
$info['remember'] = true;

$user_signon = wp_signon( $info, false );

if ( is_wp_error( $user_signon ) ) {
    $response = ( array(
        'loggedin'  => false,
        'message'   => __(),
        'error'    => $user_signon->get_error_message(),
    ) );
} else {
    $response = ( array(
        'loggedin'  => true,
        'message'   => __(),
    ) );
}

wp_send_json( $response );

}

和jQuery

jQuery(document).ready(function($) {

// Perform AJAX login on form submit
$('form#login').on('submit', function(event){

    event.preventDefault();

    $('form#login p.status').show().text(ajax_login_object.loadingmessage);

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: ajax_login_object.ajaxurl,
        data: {
            action: 'ajaxlogin',
            username: $('form#login #username').val(),
            password: $('form#login #password').val(),
            security: $('form#login #security').val()
        },
        success: function(data) {
            console.log(data);
            $('form#login p.status').text(data.message);
            if(data.loggedin == true ) {
                 $('#loginmsg').addClass('bs-callout bs-callout-success');
                document.location.href = ajax_login_object.redirecturl;
            }
            else {
                $('#loginmsg').addClass('bs-callout bs-callout-danger');
            }
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }

    });

});

});

0 个答案:

没有答案