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 a question.

I made a simply ajax function. When I'm logged in, it works perfectly. When I'm logged out, it returns me -1 (since Wordpress 3.1)

Why ? I don't understand. Precisely, it returns -1 and my entire HTML code. (lol) I'm gonna be crazy again.

PHP (in functions.php)

function say_coucou(){
check_ajax_referer( 'hello', 'nonce' );
echo "Hello";
die(); // this is required to return a proper result
exit;
}
add_action('wp_ajax_hello_hello', 'say_coucou');
add_action('wp_ajax_nopriv_hello_hello', 'say_coucou');

*JAVASCRIPT / JQUERY / AJAX (in footer.php) *

function blabla(){
    var toSend = {
      action:"hello_hello",
      post_id: "<?php echo $wp_query->post->ID; ?>",
      nonce: "<?php echo js_escape( wp_create_nonce('hello')); ?>"
    };
    url_action = 'http://www.***********/wp-admin/admin-ajax.php';
    $.ajaxSetup({cache:true});
    $.ajax({
      url: url_action,
      type:'POST',
      data: toSend,
      cache: false,
      success:function(results)
      {
        alert(results)
      }
     });
 } 

Any help, please ? Thanks.

share|improve this question
    
You have typo in nonce creation. With it fixed this works for me both logged in and logged out just fine. –  Rarst Jul 7 '11 at 15:19
    
Oops, I've rewritted it. It's my fault — Now this code doesn't work when I'm logged out =/ –  Steffi Jul 7 '11 at 15:22
add comment

2 Answers

Rarst said it worked for him both logged in and logged out, i can also confirm the same, here's my ugly test code that works, very much just a hacked together version of your code(for testing).

function say_coucou(){
    check_ajax_referer( 'hello', 'nonce' );
    echo "Hello";
    die;
}
add_action('wp_ajax_hello_hello', 'say_coucou');
add_action('wp_ajax_nopriv_hello_hello', 'say_coucou');
add_action('admin_print_footer_scripts','blabla',20000);
add_action('wp_head','enj',20000);
add_action('wp_footer','blabla',20000);
function enj() {
    wp_enqueue_script('jquery');
}

function blabla(){
?>
<script type="text/javascript">

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

        $('a#blabla').click(function(){

            var toSend = {
                action:"hello_hello",
                post_id: "1",
                nonce: "<?php echo esc_js( wp_create_nonce('hello') ); ?>"
            };
            url_action = "<?php echo admin_url('/admin-ajax.php'); ?>";
            $.ajaxSetup({cache:true});
            $.ajax({
                url: url_action,
                type:'POST',
                data: toSend,
                cache: false,
                success:function(results) {
                    alert(results)
                }
            });
        });
    });

</script>
    <a href="#" id="blabla">aaa</a>
    <?php
}

Worked both logged out and logged in(obviously not in admin logged out, because there is no admin logged out).

I'd not realistically use the hooks so poorly in real code form, so don't look to my hacky version of your code as a good example of how to do things, because it's not.. (for illustration of working test code only).

share|improve this answer
    
Ah....... But me, I use the "function blabla" in footer.php. whereas you did it in functions.php — Is there a difference ? –  Steffi Jul 7 '11 at 17:45
    
Perhaps you could post up more of your code and additionally detail exactly where your placing it? –  t31os Jul 7 '11 at 20:11
    
Okay, I updated it. Check it out –  Steffi Jul 7 '11 at 22:44
    
I did exactly the same code, it returns me "-1" (when I'm logged out) –  Steffi Jul 7 '11 at 22:57
    
You added my code from above to your functions file and still got -1 when logged out? If yes, try with plugins disabled(not sure what else it could be at this point).. –  t31os Jul 8 '11 at 8:21
add comment
up vote 1 down vote accepted

Found It!

I had done a redirect, so my ajax function status was 302... I deleted the redirect and it worked !

Thanks guys.

share|improve this answer
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.