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
show 2 more comments

5 Answers

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
    
This is the answer to the WP AJAX 0 problem. –  BenRacicot Mar 26 at 15:59
add comment

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
show 1 more comment
up vote 0 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 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 Mar 26 at 16:22
add comment

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
add comment

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
add comment

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.