error 400 bad request in Ajax with admin_ajax from wordpress

0

It turns out that I'm doing a Wordpress plugin in which I need to make an AJAX for the file "admin_ajax.php" of Wordpress, but when I send the request, the client gives me the error 400 (bad request) and I do not understand why what reason

this is my code

Javascript:

$.post(window.dibibot.ajax_uri, {
    action: "dibibot_check_message_read",
    to: (window.dibibot.USER_KEYS.split(":")[1]).toString(),
    message_id: JSON.stringify(data.message)
}, function(response) {
    console.log(response);
});

Ajax php function:

<?php
    function dibibot_check_message_read() {
        global $wpdb;
        $conversation_guid = $_POST['to'];
        $message_id = json_decode($_POST['message_id'], true);

        $conversation = $wpdb->get_var("SELECT metadata FROM " . $wpdb->prefix . "dibibot_conversations WHERE guid = '".$conversation_guid."'");
        $conversation = maybe_unserialize($conversation);
        for ($j=0; $j < count($message_id) ; $j++) { 
            for ($i=0; $i < count($conversation); $i++) {
                if($conversation[$i]['id'] == $message_id[$j]) {
                    $conversation[$i]['status'] = 2;
                    break;
                }
            }
        }
        $result = $wpdb->update($wpdb->prefix . 'dibibot_conversations', [ "metadata" => maybe_serialize($conversation) ], [ "guid" => $conversation_guid]);
        echo $result ? 1 : 0;
        wp_die(); 
    }
?>
    
asked by Anthony Medina 29.08.2018 в 18:39
source

1 answer

0

You need to declare the hooks that will make that function available

<?php 
add_action( 'wp_ajax_nopriv_dibibot_check_message_read', 'dibibot_check_message_readn' );
add_action( 'wp_ajax_dibibot_check_message_read', 'dibibot_check_message_read' );
    
answered by 29.08.2018 в 18:47