wordpress ajax for front end got 302 Found

I am new with this.. I have found out that my code works only when there is a admin logged user. how can I deal with it that it can also work without any user logged and a normal user logged.

this is my js script:

Read More
jQuery('#username').blur(function() {

jQuery('.username-massage').after('<div class="alert alert-danger usercheck-w" style="color:green;">Checking...</div>');
jQuery.ajax(
{
    url: ajax_url,
    type: "POST",
    dataType: "json",
    data: {
        action: 'checkUsernameExistentVal',
        username: jQuery(this).val(),
    },
    async: false,
    success: function (data)
    {
        alert(data + ' | '+ data.validation);
        jQuery('.usercheck-w').remove();
        if (data.validation != 'true'){
            jQuery('.username-massage').html('<div class="alert alert-danger usercheck-c" style="color:red;">' + jQuery('#username').attr('data-value') + '</div>');
            jQuery("#username").val('');
        }else{
            jQuery('.usercheck-c').remove();
        }              

    },
});

});

and this is in my functions.php in my custom plugin.

add_action('wp_ajax_checkUsernameExistentVal', 'checkUsernameExistentVal');
add_action('wp_ajax_nopriv_checkUsernameExistentVal', 'checkUsernameExistentVal');
function checkUsernameExistentVal(){
    $username = $_POST['username']; 
    $val = checkCOntactUsernameExist($username);
    $wp_users = get_users();
    $c_user = '';
    if ($val){
        $usernname_val= 'false';
    }else{
        foreach ($wp_users as $wp_user) {
            if($wp_user->user_login == $username){
                $c_user = 'true';
            }
        }
        if($c_user == 'true'){
            $usernname_val = 'false';
        }else{
            $usernname_val = 'true';
        }
    }

    echo json_encode(array("validation" => $usernname_val));
    wp_die();

}

my html code is <input type="text" name="username" id="username" value="">

does anyone have an idea about my case?

thanks in advance…

I check it in firebug and it got 302 Found

Related posts

2 comments

  1. The issue seems conflict with another plugin or function that is trying to prevent non logged in or non admin users access to the wp-admin area so it is redirecting from wp-admin/admin-ajax.php and giving you 302 response.

    Try deactivate each plugin one by one and check the issue.

  2. I put this code below the function and it works…

    if(isset($_REQUEST['action']) && $_REQUEST['action']=='ajaxFunctionMethod'){
            do_action( 'wp_ajax_' . $_REQUEST['action'] );
            do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
    }
    

Comments are closed.