Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have on frontend search box, I have implement autocomplete for it, and when I'm logged in, it works great for me.

But when I'm not logged in, I get 302 as result of ajax call, response headers have location in it, so it is trying to redirect, I do not know why. I notice a lot of posts around this issue, but none of the posts/questions didn't help me.

My Code, that works when users are logged in, but doesn't work when users are not logged in, please help.

jQuery

$.ajax({
    type: 'POST',
    url: MyAjax.ajaxurl,
    data: {
        action : 'myajax-submit',
        term : request.term,
        _ajax_nonce : MyAjax.ajax_nonce
    },
    dataType: "json",
    beforeSend: function(jqXHR, settings){
    },
    success: function(data, textStatus, jqXHR){
        response(data);
    },
    error: function(jqXHR, textStatus, errorThrown){
        response([{
            'value':'Error retriveing data',
            'id':1
        }]);
    },
    complete: function(jqXHR, textStatus){
    },
    statusCode: {                    
    }
});

php

add_action('wp_ajax_myajax-submit', 'myajax_submit');


add_action('wp_ajax_nopriv_myajax-submit', 'myajax_submit');

    function myajax_submit() {
        global $wpdb;

        //check_ajax_referer('myajax_nonce', '_ajax_nonce');

        if (isset($_POST["term"])) {
            $q = strtolower($_POST["term"]);

            .....

            $json_response = array();

            ...

            header("Cache-Control: no-cache, must-revalidate");
            header("Pragma: no-cache");
            header("Content-type: text/x-json");

            print json_encode($json_response);

            die();
        }
}
share|improve this question
1  
Do you have the final } on myajax_submit? –  m0r7if3r Mar 2 '12 at 15:20
    
@m0r7if3r Hi m0r7if3r I have, it was just copy-paste so I accidentally didn't copy last bracket. Tnx. –  user1147 Mar 2 '12 at 15:36
add comment

2 Answers

up vote 1 down vote accepted

Sounds like you have an issue 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 your 302 response.

You need to find the code that is doing this and add a conditional not to redirect if the DOING_AJAX constant is defined.

share|improve this answer
    
Hi, thank you, that was the problem. Thank you –  user1147 Mar 3 '12 at 1:40
add comment

A non-admin request... still is an admin request

Inside admin-ajax.php the constant WP_ADMIN is set to TRUE. So if you're including your files wrapped inside for example if ( ! is_admin() ), then your request will abort at the beginning of admin-ajax.php.

share|improve this answer
    
Thank you for letting me know that –  user1147 Mar 3 '12 at 1:40
    
Had to find that out the hard way myself. :) –  kaiser Mar 3 '12 at 14:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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