0

I'm having some trouble with my custom ajax function. I have this on my console log : "POST MYSITEURL/wp-admin/admin-ajax.php 302 Found". So my functions won't work...

And I really don't understand why.

I've developed a login form for the front end, it works well on my local but not online. Here is what I do :

function ajax_login_init(){
//First I register my scripts
    wp_register_script('ajax-login-script', get_template_directory_uri() . '/js/ajax-login-script.js', array('jquery'),'',true ); 
    wp_enqueue_script('ajax-login-script');
//I give them variables
    wp_localize_script( 'ajax-login-script', 'ajax_login_object', array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'redirecturl' => '/account'
    ));

}

function allowAjaxLogin(){
//I allow the function
        add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
        add_action( 'wp_ajax_priv_ajaxlogin', 'ajax_login' );

}
add_action( 'init','allowAjaxLogin' );


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

function 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'] = $_POST['remember'];

    $user_signon = wp_signon( $info, false );
    if ( is_wp_error($user_signon) ){
        echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username/password')));
    } else {
        echo json_encode(array('loggedin'=>true, 'message'=>__('Success)));
    }

    die();
}

As I said, it works well on my local version, but not online, someone can help me?

2
  • Stuff like this belongs in a plugin... and get_temlate_directory_uri() tells me you're using it in a theme.
    – kaiser
    Jun 19, 2013 at 21:10
  • Indeed, I'll try to write this as a plugin.
    – zEd.
    Jun 19, 2013 at 23:42

2 Answers 2

0

Try checking the file owner for wp-admin, it could be that it's own by different user and it won't allow you to read.

Check permission of the file if it's readable by world

2
  • I guess you're talking about CHMOD ? The file (admin-ajax.php) can be readable for the public, and it's still not working..
    – zEd.
    Jun 19, 2013 at 23:40
  • Nobody can tell me why it isn't working?
    – zEd.
    Jun 20, 2013 at 17:13
0

Finally I found why !

It was because I have 2 domain, (.com / .be)

When I update my online DataBase, I have to point to .be and not .com. It's now working !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.