带有WordPress Functions.php的Ajax始终返回0

带有WordPress Functions.php的Ajax始终返回0,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我知道这个问题已经被问了很多,所以我为任何冗余道歉。但是,似乎根本无法将此返回值设为0以外的值。下面是我在2012主题的functions.php底部包含的代码: add_filter( 'views_edit-destination', 'so_13813805_add_button_to_views' ); function so_13813805_add_button_to_views( $views ) { $views['my-button'] = '<button id

我知道这个问题已经被问了很多,所以我为任何冗余道歉。但是,似乎根本无法将此返回值设为0以外的值。下面是我在2012主题的functions.php底部包含的代码:

add_filter( 'views_edit-destination', 'so_13813805_add_button_to_views' );
function so_13813805_add_button_to_views( $views )
{
    $views['my-button'] = '<button id="update-dest-cache" type="button" class="button"             title="Update Destinations Cache" style="margin:5px" onclick="updateDestCache()">Update Destinations Cache</button>';
    $views['my-button'] .= '
        <script type="text/javascript" >
        function updateDestCache() {
            jQuery.ajax({
                type: "POST",
                url: ajaxurl,
                dataType: "json",
                action: "testAjaxFunction",
                success: function(response){ 
                    alert("Got this from the server: " + response);
                },
                error: function(MLHttpRequest, textStatus, errorThrown){  
                    alert("There was an error: " + errorThrown);  
                },
                timeout: 60000
            });    
        };
        </script>
    ';
    return $views;
}
function testAjax(){
    echo "ANYTHING!!!!";
    die();
}
add_action('wp_ajax_testAjaxFunction', 'testAjax');
add_action('wp_ajax_nopriv_testAjaxFunction', 'testAjax' );
add_filter('views_edit-destination'、'so_13805_add_button_to_views');
函数so_138105_添加_按钮_到_视图($view)
{
$views['my-button']='Update Destinations Cache';
$views['my-button'].='
函数updateDestCache(){
jQuery.ajax({
类型:“POST”,
url:ajaxurl,
数据类型:“json”,
行动:“测试功能”,
成功:功能(响应){
警报(“从服务器获得此信息:“+响应”);
},
错误:函数(MLHttpRequest、textStatus、errorshown){
警报(“有一个错误:+ERRORSHORN”);
},
超时:60000
});    
};
';
返回$views;
}
函数testAjax(){
呼应“任何事!!!!”;
模具();
}
添加动作(“wp\u ajax\u testAjax函数”,“testAjax”);
添加动作('wp_ajax_nopriv_testAjax function','testAjax');
此代码向自定义帖子类型的编辑列表中添加一个按钮,然后单击后运行该函数。按钮显示,函数运行,调用ajax函数,但0始终是响应


关于为什么会继续发生这种情况,您有什么想法吗?

ajaxurl仅在您的仪表板中定义。如果您在前端使用它,您应该声明:

$admin_url = admin_url('admin-ajax.php');
$views['my-button'] .= '
        <script type="text/javascript" >
        function updateDestCache() {
            jQuery.ajax({
                type: "POST",
                url: '. $admin_url .', 
                dataType: "json",
                action: "testAjaxFunction",
                success: function(response){ 
                    alert("Got this from the server: " + response);
                },
                error: function(MLHttpRequest, textStatus, errorThrown){  
                    alert("There was an error: " + errorThrown);  
                },
                timeout: 60000
            });    
        };
        </script>
    ';
$admin\u url=admin\u url('admin-ajax.php');
$views['my-button'].='
函数updateDestCache(){
jQuery.ajax({
类型:“POST”,
url:“.$admin_url.”,
数据类型:“json”,
行动:“测试功能”,
成功:功能(响应){
警报(“从服务器获得此信息:“+响应”);
},
错误:函数(MLHttpRequest、textStatus、errorshown){
警报(“有一个错误:+ERRORSHORN”);
},
超时:60000
});    
};
';

我的javascript写得不正确

function updateDestCache() {
   jQuery.ajax({
      type: "POST",
      url: ajaxurl, 
      data: { action: "testAjaxFunction" }, <---- THIS LINE WAS CHANGED
      success: function(response){ 
         alert("Got this from the server: " + response);
      },
      error: function(MLHttpRequest, textStatus, errorThrown){  
         alert("There was an error: " + errorThrown);  
      },
      timeout: 60000
   });    
};
函数updateDestCache(){
jQuery.ajax({
类型:“POST”,
url:ajaxurl,

数据:{操作:“测试函数”},请注意,从ajax调用中删除
url
变量和
dataType
变量会产生相同的结果。此外,导航到
/wp admin/admin ajax.php?action=testAjax
也会返回0这实际上是WordPress CMS中的一个仅管理的函数。因此,ajaxurl似乎总是被定义为我还假设这意味着
add_action('wp_ajax\u nopriv\u testAjax function','testAjax');
是不必要的代码,可以删除,但因为它不起作用,我想我会留下它。。。