Shortcodes with Ajax

Hi so I am creating shortcode so user can put it anywhere, where he wants to have some data displayed and after pressing a button (that was created by the shortcode) onClick function uses Ajax to process or display some additional information that require database input/output. However when I call ajax for example in onClick, the function itself runs but the ajax crashes with following message

POST http://localhost/wordpress/admin-ajax.php 404 (Not Found)

I have used ajax requests before and this one is no different. Javascript code:

    function buttonPressed(){
        <?php $nonce = wp_create_nonce( 'ajaxnonce' );?>
                type: "post",url: "admin-ajax.php",data: { action: 'processData', _ajax_nonce: '<?php echo $nonce; ?>' },
                success: function(html){
                    console.log("Success with html "+html);

PHP code:

add_action( 'wp_ajax_processData', 'doStuff' );
function doStuff(){
    check_ajax_referer( "ajaxnonce" );
    echo 'success';

Read more here: Shortcodes with Ajax

Leave a Reply

Your email address will not be published. Required fields are marked *