Fighting to create an ajax request. I've been reading couple of articles but still I can't get my plugin to send and recive ajax datas.

first init the js

    function ajax_load_scripts() {
        // load our jquery file that sends the $.post request
        wp_enqueue_script( "ajax-calls", plugin_dir_url( __FILE__ ) . '/assets/js/ajax_calls.js', array( 'jquery' ) );

        // make the ajaxurl var available to the above script
        wp_localize_script( 'ajax-calls', 'the_ajax_script', 
                array( 'ajaxurl' =>   plugin_dir_url('my_plugin.php') ,
                       '_ajax_nonce' => wp_create_nonce( 'my_ajax_nonce' )
                  ) );
    }
add_action('wp_print_scripts', 'ajax_load_scripts');

than trying to catch posted data in my plugins entry file

function tipps_processing_function(){
    check_ajax_referer('my_ajax_nonce');

    if ($_POST['results']){
        $token = '393a9276ae329e00b3739d2e76e52f3b';
        $tips = json_encode($_POST['results']);
        $save = new Tipspiel();
        $save->save_tips($token, $tips);
            var_dump($_POST['results']);
        return 'OK';
    }
    //do stuff here
}

add_action('wp_ajax_nopriv_add_tipps', 'text_ajax_process_request');

my js

$j("form#tiepspiel").submit(function(e) {
        e.preventDefault(); // this disables the submit button so the user stays on the page            
        var str = $j(this).serialize();
       //alert(decodeURIComponent(str));
        if ($j('ul#sortables li').size() > 10){

            $j( "#dialog-modal" ).dialog({
                    height: 140,
                    modal: true
                });
            return;
        }
        var data = {
                action: 'add_tipps',
                tips: str,
                _ajax_nonce: the_ajax_script._ajax_nonce

            };

        $j.post(the_ajax_script.ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
            //$j.cookie("tipspiel", "competitor", { expires: 1 });
           $j('form#tiepspiel').fadeOut('slow', function() {
                // Animation complete.
              });
        });              
    });

but I don't see my varibales dumped

  • Are you getting any errors in your browsers script console? Is $j your alias for jQuery? – chrisguitarguy Dec 2 '12 at 21:04
  • firebug shows no errors. alert box is empty – fefe Dec 2 '12 at 21:05
up vote 2 down vote accepted

If you need to use the WordPress enviorment and functions in your ajax processing funciton then you need to post/get your AJAX request to admin-ajax.php, So change

'ajaxurl' =>   plugin_dir_url('my_plugin.php')

to

 'ajaxurl' =>   admin_url('admin-ajax.php')
  • yes I did realized between thanks a lot – fefe Dec 2 '12 at 22:21

You have this, which will work for not-logged-in visitors:

add_action('wp_ajax_nopriv_add_tipps', 'text_ajax_process_request');

But if you don't have this too, you won't trigger the call for logged-in users (which I presume you are):

add_action('wp_ajax_add_tipps', 'text_ajax_process_request');

Most likely, your JS isn't including the 'action' parameter, or it isn't set to add_tipps. Here's an example of the JS you can use, from the codex:

<script type="text/javascript" >
jQuery(document).ready(function($) {
    var data = {
        action: 'add_tipps',
        whatever: 1234
    };
    $.post(ajaxurl, data, function(response) {
        alert('Got this from the server: ' + response);
    });
});
</script>

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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