1

I have been trying to port some previously functional ajax over to a WordPress plugin that I am helping to develop.

On the front end my javascript looks like

// this is inside a succesfully called JS function
jQuery.ajax({
   action: 'mbp-deleteFrame',
   url: mbp_Ajax.ajaxurl,
   type:"POST",
   data:{'deleteFrame':id},
   success: function(resp){
        console.log(resp);
      }
 });

In my main plugin file I also have

<?php
   function mbp_admin_scriptInsert(){
    wp_register_script('mbp_admin_scripts',
         plugins_url('/js/mbp_admin_scripts.js', __FILE__), array('jquery')  );

    wp_enqueue_script( 'mbp_admin_scripts');
    wp_localize_script('mbp_admin_scripts', 'mbp_Ajax', array(
        'ajaxurl' => admin_url('admin-ajax.php' ),

           )
     );

}

add_action('wp_ajax_mbp-deleteFrame','mbp_deleteFrame_handle' );
add_action('wp_ajax_nopriv_mbp-deleteFrame','mbp_deleteFrame_handle' );

The other function that my add_action commands refer to is

function mbp_deleteFrame_handle(){

    echo ' This is a response!';

die();   

I am just learning how to manage WP interactions with AJAX, but so far it has proved to be a fruitless process.

Right now, my understanding is that i need to pass the url of admin-ajax.php to the JS with wp_localize_script(). From what I can see I have done this, and it has been given to the AJAX call with the line url: mbp_Ajax.ajaxurl. Then, I have action: mbp-deleteFrame, as I understand it, this line should trigger either of the two add_action() functions that I have on the backend, and call "mbp_deleteFrame_handle()". From there, I would think that I would get my echo in a response, but right now, I am only getting a 0, both in my Console, and in Chrome's response tab.

Could anyone give this a second look and tell me where I am falling short?

5

the action has to be an HTTP argument, try that :

jQuery.ajax({
    url: mbp_Ajax.ajaxurl,
    type:"POST",
    data:{
        deleteFrame: id
        action: 'mbp-deleteFrame',
    },
    success: function(resp){
        console.log(resp);
    }
});

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.