尽管Ajax调用成功,但Wordpress Ajax始终返回0

时间:2017-08-30 22:45:40

标签: php jquery ajax wordpress

我正在尝试从Wordpress的!=表中读取(通过Ajax请求),并在页面中查看它。我需要你的帮助让我知道我使用Ajax成功打印根电子邮件地址所犯的错误。

我的JavaScript ajax调用是:

wp_users

function ajaxTest () { // Creating the URL that Ajax must refer to. var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>"; // Do Ajax call. jQuery.ajax({ url: ajaxurl, type: "POST", data: { action: "wp_ajax_tester" }, complete: function( xhr, status ){ console.log("Request completion status: " + status); }, success: function (response){ console.log("result: " + response); }, error: function(errorThrown){ console.log("error: " + errorThrown); } }); } 我创建了以下代码:

functions.php

我想打印根电子邮件地址,但这是浏览器控制台中打印的内容:

function app_tester()
{
    global $wpdb;
    $sql = "SELECT user_email FROM wp_users WHERE user='root'";
    $results = $wpdb->get_results($sql, OBJECT);
    echo json_encode($results;);
    wp_die();
}
add_action('wp_ajax_tester', 'app_tester');
add_action('wp_ajax_nopriv_tester', 'app_tester');

我使用Wordpress自己的jQuery,将下面的行放在文件头部分:

result: 0
Request completion status: Successful

1 个答案:

答案 0 :(得分:1)

这是Ajax的documentation,请从行动中删除wp_ajax_

示例代码

function ajaxTest ()
    {
        // Creating the URL that Ajax must refer to.
        var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";

        // Do Ajax call.
        jQuery.ajax({
            url: ajaxurl,
            type: "POST",
            data: { action: "tester" },
            complete: function( xhr, status ){
                console.log("Request completion status: " + status);
            },
            success: function (response){
                console.log("result: " + response);
            },
            error: function(errorThrown){
                console.log("error: " + errorThrown);
            } 
        });
    }