admin-ajax.php returns 0 even when the post status code is 200 OK

Question

I’ve seen a lot of similar questions and I’ve incorporated the feedback from the answers but still, the issue is not resolved.

Ajax request is sent on click of a button in js file.

WyrRoute.js

clickHandler(e) {
    var ajax_url = my_ajax_object.ajax_url
    var data ={
        'action': 'my_action',
        'question_id': '10183'
    }
    $.ajax({
        type: 'POST',
        url: ajax_url,
        data: data,
        dataType: 'json',
        success: function (xhr, x, checkStatus) {
            console.log(xhr);
            console.log(checkStatus.status)
            console.log("success")
        },
        error: function(e) {
            console.log(e.statusText);
            console.log("failure")
        }
    });
}
}

In the receiver.php file I have:

<?php 
    add_action( 'wp_enqueue_scripts', 'my_enqueue' );
    add_action( 'wp_ajax_my_action', 'my_action' );
    
function my_enqueue() {
    wp_register_script( 'ajax-script', plugins_url( '/src/WyrRoute.js' , __FILE__ ), array('jquery') );
    wp_enqueue_script('ajax-script');
    wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
function my_action(){
   $theQuestion = $_POST['question_id'];
   echo $theQuestion;
   die();
}

0
Patrick2997 1 month 2022-08-14T04:23:00-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse