skip to Main Content

I need to use AJAX in WordPress to call a third-party API, and then based on the response to then update the state of some checkboxes.

I’ve used AJAX before, but it has relied on the output of PHP function to be written back into the Ajax contents, which is not what I want here.

So my question is, at the moment this is returning a 500 error (but won’t tell me which line its own).

I have a single PHP function that does the API call and returns the result, but I’m not sure where exactly to call this in the AJAX function (should this be in action var?).

I plan to add my jQuery checkbox state changes to the success condition of the AJAX, but so far all this is doing is trying to return (echo) the output of the function underneath the AJAX code.

// AJAX query to get current status
function io_operators_get_status_ajax_enqueue() {

    // Enqueue jQuery
    wp_enqueue_script('jquery');

    // Add the AJAX inline script
    wp_add_inline_script(
        'jquery',
        '
        jQuery(document).ready(function() {
            var io_operators_status_update = function() {
                jQuery.ajax({
                    url: "/wp-admin/admin-ajax.php",
                    type: "POST",
                    data: {
                        action: "io_operators_get_current_service_state_ajax"
                    },
                    success: function(data) {
                        console.log(data);
                    },
                    error: function(throwError) {
                        console.log(throwError);
                    }
                });
            }
            io_operators_status_update();
            setInterval(io_operators_status_update.bind(null), 10000);
        })
        '
    );

}
add_action('wp_enqueue_scripts', 'io_operators_get_status_ajax_enqueue');


// The output of the AJAX contents
function io_operators_get_current_service_state_ajax() {

    try {
        return io_operators_get_current_service_state();
    } catch (Exception $e) {
        return $e;
    }
    wp_die();

}
add_action('wp_ajax_io_operators_get_current_service_state_ajax', 'io_operators_get_current_service_state_ajax');
add_action('wp_ajax_nopriv_io_operators_get_current_service_state_ajax', 'io_operators_get_current_service_state_ajax');

2

Answers


  1. The output of the API call can be printed in the ‘success’ branch of the AJAX call. however if the server replies with a 500 error, the code from the ‘error’ branch will be executed. I suggest you fix the bug you have in the API call and try again later.
    Is the API call of type POST ? That’s probably why the server is returning the 500 error

    Login or Signup to reply.
  2. In your code, you’re using the wp_ajax_* and wp_ajax_nopriv_* hooks correctly to handle the AJAX request. However, the problem lies in your io_operators_get_current_service_state_ajax function, which is not properly returning the response to the AJAX request.

    To fix this, you need to modify your io_operators_get_current_service_state_ajax function to properly handle the AJAX request and send back the response. Here’s an updated version of your code:

    // The output of the AJAX contents
    function io_operators_get_current_service_state_ajax() {
        try {
            $response = io_operators_get_current_service_state(); // Call your API function here to get the response
            wp_send_json_success($response); // Send the response back as JSON
        } catch (Exception $e) {
            wp_send_json_error($e->getMessage()); // Send the error message back as JSON
        }
    }
    
    add_action('wp_ajax_io_operators_get_current_service_state_ajax', 'io_operators_get_current_service_state_ajax');
    add_action('wp_ajax_nopriv_io_operators_get_current_service_state_ajax', 'io_operators_get_current_service_state_ajax');
    

    In the updated code, I’ve made a few changes:

    1. I removed the return statements and replaced them with wp_send_json_success() and wp_send_json_error() functions. These functions send the response back to the AJAX request in a proper JSON format.

    2. Inside the try block, I called your io_operators_get_current_service_state() function to get the response data. Make sure you have implemented this function separately to perform the API call and return the result.

    3. If an exception is caught, I used $e->getMessage() to get the error message, which is then sent back as the error response using wp_send_json_error().

    By making these changes, your AJAX request will receive the response properly, and you can then handle the checkbox state changes in the success callback of your AJAX function.

    Note: Make sure the io_operators_get_current_service_state() function is implemented correctly to perform the API call and return the desired result.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search