Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am relatively new to jQuery and AJAX in particular. I have a small issue with the return value always being 0, though I think this is actually the success message and it's not returning anything.

I have scoured the Google-verse and I have the die() function on the PHP callback and I believe the add_actions are correct.

I am working on a local host, though I doubt that affects it and this is all in the admin, not front end. I also checked that the js is enqueued and localised.

I get a 200 OK message in the chrome developer area.

I also tested out the basic AJAX from http://codex.wordpress.org/AJAX_in_Plugins and it also returned 0, which makes me wonder if it is something other than the code outlined below.

Right now I am just trying to make it send something back to the jQuery. Any help would be appreciated.

The jQuery

jQuery(document).ready(function(){
    jQuery('.cl_link_buttons').val('id').click(function() {

            var currentid = jQuery(this).attr('id');

            //alert(currentid);
            console.log(currentid);

            jQuery.ajax ( data = {
                action: 'cleanlinks_ajax_get_post_data',
                url: ajaxurl,
                type: 'POST',
                dataType: 'text',
                "currentid" : currentid

            });

            jQuery.post(ajaxurl, data, function(response) {

                var dataz = response;
                alert( dataz );
                console.log (dataz); //show json in console


            });

            return false;

    }); //end click event
}); //end doc ready

The PHP

add_action("wp_ajax_cleanlinks_ajax_get_post_data", "cleanlinks_ajax_get_post_data");
add_action("wp_ajax_nopriv_cleanlinks_ajax_get_post_data", "cleanlinks_ajax_get_post_data");

function cleanlinks_ajax_get_post_data() {

$from_ajax =  $_POST['currentid'];

echo "do" . $from_ajax . "something";

die();


}
share|improve this question
1  
Have you verified that ajaxurl is set properly? –  Andrew Bartel Apr 27 '13 at 15:14
    
Does your browser console show any errors? If so, what are they? –  s_ha_dum Apr 27 '13 at 15:17
2  
jQuery('.cl_link_buttons').val('id').click(function() looks odd. –  toscho Apr 27 '13 at 15:26
    
Andrew, yes I believe it is correct, the request url in Chrome Inspector is showing domain/wp-admin/admin-ajax.php –  Apina Apr 27 '13 at 17:08
    
@s_ha_dum No errors showing –  Apina Apr 27 '13 at 17:10

6 Answers 6

A 0 response means either that the action is not set (in the ajax data) or that the action's callback function cannot be found.

share|improve this answer
    
Yeah, this is the correct answer. All adding die() to the end does, is terminate the script. That answer is technically correct if you're seeing 0 appended to the END of the output, however if all you get is '0', then it means nothing was returned, and you have an error as described in this answer. –  Hybrid Web Dev May 11 at 20:26

What you have to do is add die();at the end of your function.

See it here: http://codex.wordpress.org/AJAX_in_Plugins

It worked for me.

share|improve this answer
1  
This is the answer to the WP AJAX 0 problem. –  BenRacicot Mar 26 at 15:59

I got same problem. And solved it. You must send "action" variable like in example:

var dataString = {lat: '55.56', lng: '25.35', action:'report_callback'};
 $.ajax({                            
        url: "http://domain.net/wp-admin/admin-ajax.php",  
        type: "POST",
        //some times you cant try this method for sending action variable
        //action : 'report_callback',
        data:dataString,        
        success: function(data){ 
            console.log(data);

            },
        error: function() {
            console.log("Error");            
        }
    });

Because in wp-admin/admin-ajax.php is handler for action variable:

if ( empty( $_REQUEST['action'] ) ) {...}
Line 26
share|improve this answer
1  
The OP does send an action parameter. While this may have worked for you it was not the problem here. –  s_ha_dum May 11 at 20:19

Try running this code on the console

jQuery.post(ajaxurl, {action:'cleanlinks_ajax_get_post_data'}, function(response) {
     console.log (response);
});

I can see many things wrong about your JavaScript code and that might be the reason.

share|improve this answer
    
Well it is coming up with a lot of things I dont fully understand. What I do understand: ReadyState 4, status 200, responseText "0". And then it comes up with the response 0. If there something in specific I should be looking for here? If there are issues with the code, please point them out and I can look into them, I am still learning jQuery. –  Apina Apr 27 '13 at 17:34
    
do you have your site running live? –  Omar Abid Apr 27 '13 at 17:35
    
No, it's localhost –  Apina Apr 27 '13 at 17:36
    
Hard to tell. Could you try running console.info(ajaxurl); and see what it gives? –  Omar Abid Apr 27 '13 at 17:37
    
/wp-admin/admin-ajax.php is the response edit -- /wp-admin/admin-ajax.php undefined –  Apina Apr 27 '13 at 17:45
up vote -1 down vote accepted

So I worked it out. It was not the jQuery as such though I have improved that, it was the placement of the call back function. I moved it over to the main plugin file and it worked.

share|improve this answer
1  
can you show how did you do it ? –  Francisco Corrales Morales Jan 27 at 20:39
    
I am having this same issue, could you show how you fixed this? –  Jeremy Mar 8 at 18:31
    
Another answer is here: wordpress.stackexchange.com/a/131397 @Jeremy –  Francisco Corrales Morales Mar 26 at 16:22

Try adding an if statement:

function my_function(){
$id = $_POST['variation_id'];

    if(isset($_POST['variation_id'])) { 


//your coded function


die();
}



}// end function
share|improve this answer
1  
How would that solve the problem? Note the accepted answer and the original code. –  toscho Sep 23 '13 at 2:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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