1

I'm using ajax in wordpress. Here is my ajax code :

var name = "Toto";
$.ajax({
        url : ajaxurl,
        type : 'post',
        data : {
            'action' : 'test_action',
            'data' : {
                'name' : name,
            }
        },
        success : function(response){
            alert(response);
        },
        error : function(){
            alert('error');
        }
    });

My PHP code

add_action("wp_ajax_test_action", "_test_function");
function _test_function(){
    wp_localize_script( 'edit-taxonomy' , 'ajax_object' , array('ajaxurl' => admin_url('admin-ajax.php')));
    die('success');
}

then I inspected it on browser and I get :

enter image description here

I get an alert with '0' value (as I did in success case). Then I realize that it doesn't call _test_function function (it should return 'success'). I think it's port problem because with other similar code, port value is 80 and it works, but I don't understand why is it using another port, and how to fix it.

  • There are several questions in this about ajax returning zero, have you checked them? By the way, I don't understand the "port problem", what do you mean? If the response code status is 200, I don't think you have a port problem. Where is that ajax call performed, in admin side or frontend? – cybmeta Dec 16 '16 at 9:08
  • what I mean in port problem is that I used similar functions with ajax, and I get "Remote Address: 127.0.0.1:80" , but in this function I get "Remote Address: 127.0.0.1:9614" and my function is not called. – Dina Dec 16 '16 at 9:56
  • that ajax call is performed in frontend – Dina Dec 16 '16 at 9:57
1

In general, when a ajax call usgin admin-ajax.php returns zero it means that the action is not set in the request data, or that the action callback is not found.

In your case, it seems that the action callback is not found.

When performing ajax call in the frontend, you need to use the action wp_ajax_nopriv_{action} instead of wp_ajax_{action}:

// For admin side
add_action("wp_ajax_test_action", "_test_function");
// For frontend side
add_action("wp_ajax_nopriv_test_action", "_test_function");
function _test_function(){
    die( 'success' );
}

Also, localizing script in ajax action callback has not much senses, you need to do it some where else, usually in wp_enqueue_scripts action:

add_action( 'wp_enqueue_scripts', 'cyb_enqueue_scripts' );
function cyb_enqueue_scripts() {

    // First, enqueue you ajax script
    wp_enqueue_script( 'my-script', plugins_url( 'assets/js/mi-ajax-script.js', __FILE__ ) );

    // Second localize the script to pass variables
    wp_localize_script(
         'my-script' , // handle of the script to be localized
         'ajax_object' ,
          array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
     );

}

Then, in my-ajax-script.js you can access to ajax_object created with wp_localize_script():

var name = "Toto";
$.ajax({
        url : ajax_object.ajaxurl,
        type : 'post',
        data : {
            'action' : 'test_action',
            'data' : {
                'name' : name,
            }
        },
        success : function(response){
            alert(response);
        },
        error : function(){
            alert('error');
        }
});
  • 1
    Thanks a lot, it works. Adding nopriv in add_action resolved it – Dina Dec 16 '16 at 10:22

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.