0

Below is the code for the AJAX File. alert(cont) is given the correct result but the problem is with the response. I am just getting error as the response.

function updateRadioButton(rating,pid){
    var cont = rating + '-' + pid;
    alert(cont);
    jQuery.ajax({
        type: 'POST',  
        url: TihomAjax.ajaxurl, 
        data: {"action": "tihom_rating_action", "contd":cont},
        success: function(data){ 
            if (data.success) {
                alert("success");
            } else {
                alert("error");
            }
        }
    }); 
}

Code for the tihom_rating_action function and other hooks in Wordpress are

wp_enqueue_script('tihom_ajax', plugins_url( '/js/tihom_ajax.js' , __FILE__ ) , array( 'jquery' ));
    wp_localize_script( 'tihom_ajax', 'TihomAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php') ));

    function tihom_rating_action() {         
        echo "hello";
        wp_die();
    }
    add_action( 'wp_ajax_my_action', 'tihom_rating_action' );
    add_action( 'wp_ajax_nopriv_my_action', 'tihom_rating_action' );

1 Answer 1

0

In the JavaScript, you're using "action": "tihom_rating_action", but in PHP you're using wp_ajax_nopriv_my_action. Instead of my_action you need to use tihom_rating_action as well.

4
  • thanks for your reply. I corrected the codes as per your points. But still getting the same error.
    – Mohit
    Feb 6, 2018 at 10:22
  • Have you tried using die() instead of wp_die()? Have you tried debugging the actual ajax response in your browser's developer tools (F12)? What does the response say?
    – swissspidy
    Feb 6, 2018 at 10:25
  • updated die() to wp_die() still getting same error. Yes, i have tried there is no error in the ajax code. alert(cont); in ajax file gives correct response but the final response from the tihom_rating_action is giving an error.
    – Mohit
    Feb 6, 2018 at 10:29
  • You're JS is looking for data.success, but you're not sending JSON with a success property. Feb 6, 2018 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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