0

I am trying to make an AJAX call for a plugin.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$.post(
    '<?php admin_url('admin-ajax.php'); ?>',
    {
        action : 'AAA'
    },
    function( response ) {
        alert( "[[[[" + response + "]]]]" );
    }
);
}

The ajax call is working fine.

add_action( 'wp_ajax_nopriv_AAA', 'fAAA' );
add_action( 'wp_ajax_AAA', 'fAAA' );

function fAAA(){
      echo "123";
      die();
}

For some reason my fAAA is not getting called.

I used var_dump( $wp_filter['wp_ajax_nopriv_AAA'] ); to check the actions. * in the plugin, it showed that the action is added * when i looked in admin-ajax.php, the action is not there.

Any help?

4

Aside from @Tareq answer (which is your main problem), you should

  • wp_register_script and the wp_enqueue_script
  • load your local copy of jQuery instead of the one from Google CDN
  • localize your script using wp_localize_script
2

You didn't echo admin_url('admin-ajax.php')

0

You need to avoid using $ in wordpress, as by default, Wordpress runs jQuery in something called "no conflict" mode. I have learnt to keep to jQuery instead of $ when developing with Wordpress.

I don't know if it's your problem or not, as I'm browsing to find a fix for a problem of my own. You may even know this and be working around it. Apologies if so.

  • In this case, $ is passed as an argument to the function. This is one of the methods outlined in the wp_enqueue_script codex page and works correctly in WordPress. – Milo May 9 '17 at 12:51

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.