【问题标题】:Wordpress Ajax returns full html page from functions.php, not echoWordpress Ajax 从functions.php 返回完整的html 页面,而不是回显
【发布时间】:2012-04-18 20:41:28
【问题描述】:

我正在使用 wordpress ajax 调用从 wordpress 主题 functions.php 中的函数返回简单内容。但是,会返回一个完整的 html 页面。

这里是ajax调用

<?php   
$ajax_nonce = wp_create_nonce("iwhq_beginner_select_course");
?>

<script type="text/javascript" language="javascript">
jQuery(document).ready(function(){

jQuery("#beg_golf_course").change(function() {  //do this when course changes

//in Wordpress ajaxurl always points to admin-ajax.php
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
var course_id = 4; 

//Do the ajax
    jQuery.ajax({
        type: "POST", 
        url: ajaxurl,

        //NOTE - the action parameter calls the function in functions.php
        data: { action: 'select_course_aj', course_id: course_id, _ajax_nonce: '<?php echo $ajax_nonce; ?>' },


        //display alert on success
        success: function(html){ 
            alert(html);
        } 
    }); //close jQuery.ajax(
    return false;

    });
});
</script>

这是functions.php中的函数

function select_course_func(){
echo $_POST["course_id"];
die();
}

add_action('wp_ajax_select_course_aj','select_course_func');

包含 jquery ajax 调用的页面的 HTML 实际上显示在警报中而不是回显中。

有哪位大神能告诉我原因吗?

谢谢 标记

【问题讨论】:

  • 我确实在functions.php中的select_course_func PHP调用中包含了nonce调用,但得到了相同的结果。 check_ajax_referer('iwhq_beginner_select_course', '_ajax_nonce');
  • 您查看过 Firebug 中的请求/响应吗?
  • 那么究竟什么是警报?你期待'4'对吗?发生了什么是您在加载和 html 404 页面上使用 html 函数的 div?检查您发送 ajax 调用的 url。就像 Jay Blanchard 所说,您可以在 firebug 或 chrome 的网络选项卡中轻松做到这一点。
  • 这是我在 Chrome 中检查网络选项卡时的结果:irishwebhq.com/wp-content/uploads/2012/04/chrome_network.jpg 我不明白为什么它会给我 admin.php 的 302 - 我在我的代码中测试了这个链接,它链接到 @ 987654322@
  • 是的,John B,我期待 4 发出警报,但发出 ajax 调用的完整 HTML 页面显示在警报中。

标签: php jquery ajax wordpress


【解决方案1】:

好的,问题解决了。看看我上面的最后 3 个 cmets 加上...

!defined('DOING_AJAX') 是一个常量,可用于测试用户是否在执行 ajax 请求。我将此与将非管理员重定向到前端的逻辑结合起来,它现在可以工作了。

/* check the role of current loged in user for redirection */
add_action('admin_init','rt_checkRole');
function rt_checkRole() {

    global $wp_roles;
    $currentrole ='';
    foreach ( $wp_roles->role_names as $role => $name ) {
        if ( current_user_can( $role ) ){
                    $currentrole = $role;
                }
        }
        if(!defined('DOING_AJAX') && (!$currentrole || ($currentrole != 'administrator' && $currentrole != 'editor'))){
            wp_redirect (site_url().'/front-end-login/');
        }
}

在https://wordpress.stackexchange.com/questions/26100/redirect-out-of-wp-admin-without-losing-admin-ajax-php找到有关 !defined('DOING_AJAX') 的信息

感谢所有评论的人。

【讨论】:

  • 但是为什么最初返回的所有HTML?
  • 我要添加哪个文件?
【解决方案2】:

如果您想为非管理员用户进行 ajax 调用,您应该使用下面的代码,这将禁止非管理员用户访问 wp-admin,但允许每个用户(登录或注销用户)进行 ajax 调用

function my_admin_init(){
    if( !defined('DOING_AJAX') && !current_user_can('administrator') ){
        wp_redirect( home_url() );
        exit();
    }
}
add_action('admin_init','my_admin_init');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2021-06-02
    • 2015-07-15
    • 2015-03-25
    • 2013-07-19
    • 2015-09-21
    • 2014-10-10
    相关资源
    最近更新 更多