0

My problem is: Can't do a POST request with jQuery. Down here is the complete description:

I created a link in a page (using the plugin PHP code for posts and pages):

<?php
    $nonce = wp_create_nonce("test_nonce");
    $link = admin_url('admin-ajax.php?action=my_delete_object&object_id='.$object_id.'&nonce='.$nonce);

    echo "<a data-object_id=\"".$object_id."\" data-nonce=\"".$nonce."\" href=\"".$link."\" class=\"delete-object\" >Click Here!</a>
?>

Then I added this to my child theme's functions.php:

wp_enqueue_script( 'js_functions', get_bloginfo( 'stylesheet_directory' ) . '/js/functions.js', array( 'jquery' ), false);
wp_localize_script( 'js_functions', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

add_action("wp_ajax_my_delete_object", "my_delete_object");
add_action("wp_ajax_nopriv_my_delete_object", "my_must_login");


function my_delete_object() {
    echo "<script> alert('test: ajax request reached action!') </script>";
    // nonce check and other actions;
    $result['type'] = "success";
    die(); 
}

function my_must_login() {
    //tell user to log in;
    $result['type'] = "success";
    die();
}

If I click the link, I get the alert ("test: ajax request reached action!") correctly and, if I add other actions to my_delete_object, they take place as well.

The problem started when I wanted to add an ajax request to the scene.

In my child theme's folder, I created the folder js, and put the file functions.js inside with the content:

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

   $(".delete-object").click( function(event) {
      event.preventDefault();

      object_id = $(this).attr("data-object_id");
      nonce = $(this).attr("data-nonce");

      $.post({
         url : MyAjax.ajaxurl,
         data : {action: 'my_delete_object', object_id: object_id, nonce: nonce},
         success: function(response) {
            if(response.type == "success") {
               alert("Ajax request returned success!");
            }
            else {
               alert("Ajax request returned something else.");
            }
         },
         error: function(){
              alert("Ajax request no good");
         }
      });   
      alert('click triggered action!');
   });

 });

Then it does nothing when I click the link. It just pops the alert "click triggered action!", and nothing else.

My conclusion is that the POST is not happening. Any ideas why?

3
  • 1
    Can you confirm that you're logged in? You only have my_delete_object() hooked to wp_ajax, so it will only fire for logged in users. It looks like you're trying to do something different if someone isn't logged in, but you need to specify which hook to fire in the action attribute of the data object in your $.post(). So, in this instance, my_delete_object() will only fire for logged in users and my_must_login() will never fire, because you aren't passing it as the action. Commented Aug 8, 2014 at 16:34
  • @AndrewBartel, that's a very good point! There's no way I would reach my_must_login with this ajax request. Then, I duplicated the $.post(...) part and changed the action of the duplicate to my_must_login so that any of the actions should be reached (even though I can assure the user is logged). Sadly, there was still no post with ajax. If I take off the javascript code, it works with the link. I'm still wondering why the POST not working. :( Commented Aug 8, 2014 at 17:32
  • I discovered that the problem was that I'm using $.post(). If I change it to $.ajax() it works...Still don't know why, but it does. The problem now, is that I still get the variable response as undefined. How should I send it? Commented Aug 8, 2014 at 18:38

1 Answer 1

0

I solved my problem after a long time struggling, that's why I think I should list some tips for someone with a problem like this:

  • If you change your javascript file, use ctrl+f5 to force reload the page and the resources. The caching almost got me crazy as I changed the file and the changes didn't take place.
  • Use your browser's network analisys tool. In chrome, hit f12 and go to the 'network' tab. Make your submission and click to see the details. Firstly, I couldn't see what the server was responding and that tool helped me a lot.
  • Use wp_send_json_success( $data ); in your admin-ajax.php and set dataType : 'json' on the ajax call.
  • Create the actions for users that are logged in users, and for users that aren't. If you forget to create the action, it may not work. In my case, it was the line: add_action("wp_ajax_nopriv_my_delete_object", "my_must_login");

This guidelines would have spared me some hair! :)

2
  • 1
    Quick note about dev consoles, chrome is by far the superior one and has the option to disable caching while it's open. Commented Aug 11, 2014 at 2:32
  • oh, that will help a lot! Sometimes I forget to ctrl+f5. To disable caching: stackoverflow.com/questions/5690269/… Commented Aug 11, 2014 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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