doupao2277
2012-04-18 20:41
浏览 122
已采纳

Wordpress Ajax从functions.php返回完整的html页面,而不是echo

I am using a wordpress ajax call to return simple content from a function in wordpress theme functions.php. However, a full html page is returned instead.

Here is the ajax call

<?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>

And this is the function in functions.php

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

add_action('wp_ajax_select_course_aj','select_course_func');

The HTML of the page containing the jquery ajax call is actually displayed in the alert instead of the echo.

Any geniuses out there able to tell me why?

Thanks Mark

图片转代码服务由CSDN问答提供 功能建议

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

这是ajax调用

 &lt;?php 
 $ ajax_nonce = wp_create_nonce  (“iwhq_beginner_select_course”); 
?&gt; 
 
&lt; script type =“text / javascript”language =“javascript”&gt; 
jQuery(document).ready(function(){
 
jQuery(“#  beg_golf_course“)。change(function(){//在课程更改时执行此操作
 
 //在Wordpress中ajaxurl始终指向admin-ajax.php 
var ajaxurl ='&lt;?php echo admin_url('admin-ajax  .php');?&gt;'; 
var course_id = 4; 
 
 //执行ajax 
 jQuery.ajax({
 type:“POST”,
 url:ajaxurl,
 
  //注意 -  action参数调用functions.php 
 data中的函数:{action:'select_course_aj',course_id:course_id,_ajax_nonce:'&lt;?php echo $ ajax_nonce;?&gt;'},
 
  
 //成功时显示警告
成功:函数(html){
 alert(html); 
} 
}); //关闭jQuery.ajax(
返回false; 
 
})  ; 
}); 
&LT; /脚本&GT; 
   
 
 

一个 这是函数中的函数.php

  function select_course_func(){
echo $ _POST [“course_id”]; 
die(); 
} 
 
add_action  ('wp_ajax_select_course_aj','select_course_func'); 
   
 
 

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

那里有任何天才能告诉我原因吗?

谢谢 Mark

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dreljie602951 2012-04-19 14:24
    已采纳

    OK, Problem solved. See my last 3 comments above plus...

    !defined('DOING_AJAX') is a constant that can be used to test that the user is not performing an ajax request. I combined this with my logic for redirecting non-admins to the frontend and it works now.

    /* 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/');
            }
    }
    

    Found out about !defined('DOING_AJAX') at https://wordpress.stackexchange.com/questions/26100/redirect-out-of-wp-admin-without-losing-admin-ajax-php

    Thanks to all who commented.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dquv73115 2014-04-30 12:04

    If you want to do ajax call for non admin user you should use the code below, that will disallow wp-admin access for non admin user but allow ajax call for every user, logged in or logged out user

    function my_admin_init(){
        if( !defined('DOING_AJAX') && !current_user_can('administrator') ){
            wp_redirect( home_url() );
            exit();
        }
    }
    add_action('admin_init','my_admin_init');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题