简体   繁体   English

Wordpress Ajax调用在自定义页面上不起作用

[英]Wordpress Ajax call not working on custom page

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. 到目前为止,我已经验证了它可以在我创建的插件中找到PHP和JS文件,甚至可以生成XML输出。 That I can see if I let the default <a> tag behavior. 我可以看到是否允许默认的<a>标记行为。

Somehow it blocks where the output is generated. 它以某种方式阻止了生成输出的位置。

Thanks 谢谢

This is for a custom page. 这是一个自定义页面。

1 - Here is the HTML: 1-这是HTML:

Link to trigger ajax: 链接以触发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 2-插件目录中的PHP代码

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 3-同一插件目录上的jQuery代码

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);
        }
    });     
});
});

You have to let wp know who can use the ajax calls. 您必须让wp知道谁可以使用ajax调用。

wp_ajax is for the admin section wp_ajax用于管理部分

add_action('wp_ajax_process_ajax_input', 'callBackFunction_Name');

wp_ajax_nopriv is for non admin (users) wp_ajax_nopriv适用于非管理员(用户)

add_action('wp_ajax_nopriv_process_ajax_input', 'callBackFunction_Name');

This is the call back function that will then process your ajax call. 这是回调函数,它将处理您的ajax调用。

function callBackFunction_Name{

    $allFields = $_REQUEST;
    #todo process input.
}

I finally found what the issue was. 我终于找到了问题所在。 Simply, on the ajax call, the data type should be 'xml' instead of 'html. 简而言之,在ajax调用中,数据类型应为“ xml”而不是“ html”。

The WP_Ajax_Response generates an XML file, so one needs to adapt the ajax call accordingly. WP_Ajax_Response生成一个XML文件,因此需要相应地调整ajax调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM