ajax在wordpress中发送400错误请求错误

Nag*_*tna 4 javascript php ajax wordpress jquery

大家好,我正在尝试在 wordpress 中使用 ajax,但遇到了一些问题。我的代码在控制台中给了我一个错误

    jquery-3.3.1.js:9600 GET http://localhost/wordpress/wp-admin/admin-ajax.php 400 (Bad Request)
send @ jquery-3.3.1.js:9600
ajax @ jquery-3.3.1.js:9206
(anonymous) @ main.js?ver=1:27
dispatch @ jquery-3.3.1.js:5183
elemData.handle @ jquery-3.3.1.js:4991
Run Code Online (Sandbox Code Playgroud)

我查看了源代码,它向我显示了这一行的错误

xhr.send( options.hasContent && options.data || null );
Run Code Online (Sandbox Code Playgroud)

这是我的 jquery

$('#retrieve_users').click(function() {

    $.ajax({
            type: "GET",
            url: scriptData.ajaxurl,
            action : 'retrieveUsersData',
            dataType: "html",   //expect html to be returned                
            success: function(response)
            {                    
                $("#users_data").html(response); 
                //alert(response);
            }
        });
});
Run Code Online (Sandbox Code Playgroud)

这是我在functions.php文件中的php代码

function retrieveUsersData(){

echo "ajax responding";
}

add_action('wp_ajax_retrieveUsersData','retrieveUsersData');
add_action('wp_ajax_nopriv_retrieveUsersData', 'retrieveUsersData');
Run Code Online (Sandbox Code Playgroud)

这是我的 html

<button id="retrieve_users">Watch Users</button>
                <div id="users_data"></div>
Run Code Online (Sandbox Code Playgroud)

请帮忙!!我不知道如何使用 wp_enque_scripts 在 wordpress 中导入 jquery src 链接,所以我直接将 with jquery src 粘贴到 html 中。我希望它不会造成问题

请帮助..我真的很感激。谢谢

Ana*_*vol 6

更改要使用的代码,POST并在请求正文中使用操作键:

jQuery.ajax({
            url: scriptData.ajaxurl,
            type: "POST",
            dataType: 'html',
            data: {
                action: 'retrieveUsersData'
            },

            ...
});
Run Code Online (Sandbox Code Playgroud)

您还应该wp_die()在函数中使用以防止0被回显。

function retrieveUsersData(){
    echo "ajax responding";
    wp_die();
}
Run Code Online (Sandbox Code Playgroud)