簡體   English   中英

ajax在wordpress中發送400錯誤請求錯誤

[英]ajax sending 400 bad request error in wordpress

大家好,我正在嘗試在 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

我查看了源代碼,它向我顯示了這一行的錯誤

xhr.send( options.hasContent && options.data || null );

這是我的 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);
            }
        });
});

這是我在functions.php文件中的php代碼

function retrieveUsersData(){

echo "ajax responding";
}

add_action('wp_ajax_retrieveUsersData','retrieveUsersData');
add_action('wp_ajax_nopriv_retrieveUsersData', 'retrieveUsersData');

這是我的 html

<button id="retrieve_users">Watch Users</button>
                <div id="users_data"></div>

請幫忙!! 我不知道如何使用 wp_enque_scripts 在 wordpress 中導入 jquery src 鏈接,所以我直接將 with jquery src 粘貼到 html 中。 我希望它不會造成問題

請幫助..我真的很感激。 謝謝

更改您的代碼以使用POST並在請求正文中使用操作鍵:

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

            ...
});

您還應該在函數中使用wp_die()來防止0被回顯。

function retrieveUsersData(){
    echo "ajax responding";
    wp_die();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM