Success not being returned from ajax post

AJAX Post is not returning success call in wordpress. I have the following code and I can get to the first dialog box in testing, but no mater what I do its not getting to the second. It’s not finding the function in functions.php even though I have it declared.

    jQuery(document).ready(function(){
    jQuery("#send_btn").click(function(){

    var datastring = $("#redemmpointsForm").serialize();

      var points = $('#points').val();
      var comments = $('#comments').val();

          jQuery.ajax({
             type : "post",
             dataType : "json",
             url : myAjax.ajaxurl,
             data : {"action": "redeempoints", "points":points},
             success: function(response) {
                if(response.type == "success") {
                 alert('do i get here');
                }
                else {
                   // Do something else
                }
             }
          });
    });
     }); //Modal event Ends

functions.php file

Read More
wp_localize_script( 'inkthemes', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php')));

function functionRedeempoints() {

die();
return true;
}

add_action("wp_ajax_functionRedeempoints", "functionRedeempoints");
add_action("wp_ajax_nopriv_functionRedeempoints", "functionRedeempoints");

Ok so i treid the following

jQuery(document).ready(function(){
jQuery("#send_btn").click(function(){

var points = jQuery('#points').val();
var comments = jQuery('#comments').val();

         var allData = {
   action: 'functionRedeempoints',
   points: points,
   comments:comments
}
       var data = JSON.stringify(allData);   

           alert( data);

          jQuery.ajax({
             type : "post",
             dataType : 'json',
             url : myAjax.ajaxurl,
             data : data,
             success: function(response) {
            if(response.success) {
             alert('do i get here');
            }
            else {
               // Do something else
            }
         }
      });
});
 }); //Modal event Ends

And iN MY FUCNTIONS php Its like its not fidning the php function.

  wp_localize_script( 'inkthemes', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php')));

function functionRedeempoints() {
wp_send_json_success(true);
}

add_action("wp_ajax_redeempoints", "functionRedeempoints");
add_action("wp_ajax_nopriv_redeempoints", "functionRedeempoints");

Related posts

1 comment

  1. The problem is that your function functionRedeempoints does not return anything that the ajax call can handle.

    It just dies even before the return statement.
    Also a return by the PHP end will never actually be interpreted by the JS. JS can only read from the http request, so you need to actually write to it by an echo statement.

    WordPress provides a convenient way of handling this for you:
    What you need would be something like:

    function functionRedeempoints() {
       wp_send_json_success(true);
    }
    

    This already takes care of stopping execution and properly JSON encoding your response.

    Also the correct response handling on the JS side is a little different than in your example code.
    You can find the details on this here:
    https://codex.wordpress.org/Function_Reference/wp_send_json_success

    But what it boils down to is that the success is encoded in the result.success property of the response.

    Hence you want your check to be

    if(response.success)
    

    instead of if(response.type == "success")

    With these changes your example should work though 🙂

    Working example ( in plugin form) based on your code:

    Put this in hello.php in the plugins folder

    <?php
    
    /*
    Plugin Name: Ajax demo
    */
    
    function test_load_js() {
        wp_register_script( 'ajax_demo', plugins_url( 'hello.js' ), array( 'jquery' ) );
        wp_localize_script( 'ajax_demo', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
        wp_enqueue_script( 'ajax_demo' );
    }
    
    function functionRedeempoints() {
    
        wp_send_json_success( true );
    }
    
    add_action( "wp_ajax_functionRedeempoints", "functionRedeempoints" );
    add_action( "wp_ajax_nopriv_functionRedeempoints", "functionRedeempoints" );
    add_action( "init", "test_load_js" );
    if ( ! defined( 'DOING_AJAX' ) ) {
        echo '<input type=button value="send" id="send_btn">';
    }
    

    Put this in hello.js in the plugins folder

    jQuery(document).ready(function () {
        jQuery("#send_btn").click(function () {
    
            var points = jQuery('#points').val();
            var comments = jQuery('#comments').val();
    
            var data = {
                action: 'functionRedeempoints',
                points: points,
                comments: comments
            };
    
            alert(JSON.stringify(data));
    
            jQuery.ajax({
                type: "post",
                dataType: 'json',
                url: MyAjax.ajaxurl,
                data: data,
                success: function (response) {
                    if (response.success) {
                        alert('do i get here');
                    }
                    else {
                        // Do something else
                    }
                }
            });
        });
    });
    

    Hope this helps you to get a start here, works just fine when you click the button that should appear on the upper left of your admin screen ( zoom in 😉 )

Comments are closed.