WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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
1  
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 '14 at 20:26
    
Or you just returned nothing on purpose in the php that handles the ajax request. Be sure to echo something out, otherwise, use .always to capture it. – Solomon Closson Oct 26 '15 at 17:12

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

See the reason and more here: http://codex.wordpress.org/AJAX_in_Plugins

share|improve this answer
5  
This is the answer to the WP AJAX 0 problem. – BenRacicot Mar 26 '14 at 15:59
    
Actually, if you just add die() without echoing something out, this will also give you a 500 Internal Server Error, and return 0 for wp-admin/admin-ajax.php. You should always echo out something, even if you are just setting values and nothing is needed to be returned. Otherwise, if you echo nothing and die(), you have to use .always() to capture it, cause it will not be in .done(), it will be in .fail() because it dies without anything = 500 Error. – Solomon Closson Oct 26 '15 at 17:05
    
do you have some links, or working code, so we can take a look ? @SolomonClosson – Francisco Corrales Morales Oct 26 '15 at 17:18
    
All my answers have been tested in live environments. It's very simple to test this, just do a die(); in the ajax function in the functions.php file without echoing out anything prior to this, and call the action via ajax, e.g.: var testing = $.ajax( ... ); testing.fail(function(response) { console.log('Failed ' + response); }); testing.done(function(response) { console.log('Success ' + response); }); testing.always(function(response) { console.log('Ajax Request complete: ' + response); }); – Solomon Closson Oct 26 '15 at 19:33
    
You will notice, Failed will show up, the response will be a 500 Internal Server Error. – Solomon Closson Oct 26 '15 at 19:38

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
1  
try replacing ajaxurl with 'localhost/wp-admin/admin-ajax.php'; and see what it gives – Omar Abid Apr 27 '13 at 18:57

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 '14 at 20:19

I had this problem too, and it was the fact that I was using return instead of echo in my PHP function. Changing it to echo fixed it.

function doAjax() {
    $result = getPosts();
    echo json_encode($result, true);
    die();
}
share|improve this answer
up vote -2 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 '14 at 20:39
    
I am having this same issue, could you show how you fixed this? – Jeremy Mar 8 '14 at 18:31
    
Another answer is here: wordpress.stackexchange.com/a/131397 @Jeremy – Francisco Corrales Morales Mar 26 '14 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.