doutang1884 2017-04-03 14:58
浏览 76

Wordpress Ajax调用无法在自定义页面上工作

All seems in order, but still not getting destination populated.

So far I've verified that it can find the PHP and JS files within the plugin I created and even generate the output in XML. That I can see if I let the default <a> tag behavior.

Somehow it blocks where the output is generated.

Thanks

This is for a custom page.

1 - Here is the HTML:

Link to trigger ajax:

$link = admin_url('admin-ajax.php?action=pay_dialog_step1&boo_zone='.$palier['palier']);

<a href="' . $link . '" id="' . $palier['palier'].'" class="do_popup btn btn-lg">'.get_option('_boopass_buybtn_label', '').'</a>

Division to be populated:

<div id="alloconv_popup"></div>

2- PHP code in a plugin directory

add_action("wp_ajax_pay_dialog_step1", "pay_dialog_step1");

// ajax call
    function pay_dialog_step1(){
        $boo_zone = $_REQUEST['boo_zone'];
        $response = new WP_Ajax_Response;
        $html = '<div class="bp_entry_wrapper">
                            <div class="entry normal" >
                                <div class="alloconv_palier_header" >
                                    <p> Alloconv ' . get_option('alloconv_' . $_REQUEST['boo_zone'] .'_token', '') . ' tokens </p>
                                </div>';                        
        $html.= get_option('alloconv_' . $_REQUEST['boo_zone'] .'_script', '');                                     
        $html.=     "</div>";
        $html.=     "</div>";

        $response->add( array(
            'data'  => 'success',
            'supplemental' => array(
                'boo_zone' => $boo_zone,
                'message' => $html,
            ),
        ) );        
        $response->send();
        exit(); 
    }

add_action('init', 'ajax_popup_script' );

function ajax_popup_script() {
wp_register_script( "ajax_popup_script", WP_PLUGIN_URL.'/ajax-popup-paiement/ajax_popup_paiement.js', array('jquery') );
wp_localize_script( 'ajax_popup_script', 'ajaxPaiement', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'ajax_popup_script' );

}   

3- jquery code on same plugin directory

jQuery(document).ready( function() {

jQuery(".do_popup").click( function(e) {
    e.preventDefault();
    var link = this;
    var boo_zone= jQuery(link).attr("id");
    var info = {
        action: 'pay_dialog_step1',
        boo_zone: boo_zone
    };

    // Post to the server
    jQuery.ajax({
        type:"POST",
        url:ajaxPaiement.ajaxurl,
        data:info,
        dataType:html,
        success: function(data){        
             jQuery("#alloconv_popup").html(data);
        }
    });     
});
});
  • 写回答

2条回答 默认 最新

  • dongwenyou4298 2017-04-03 15:33
    关注

    You have to let wp know who can use the ajax calls.

    wp_ajax is for the admin section

    add_action('wp_ajax_process_ajax_input', 'callBackFunction_Name');
    

    wp_ajax_nopriv is for non admin (users)

    add_action('wp_ajax_nopriv_process_ajax_input', 'callBackFunction_Name');
    

    This is the call back function that will then process your ajax call.

    function callBackFunction_Name{
    
        $allFields = $_REQUEST;
        #todo process input.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?