为什么此AJAX调用运行不一致?

时间:2019-04-16 15:12:34

标签: php woocommerce

我正试图保存人们暂停其WooCommerce订阅的原因。我为此制作了一个自定义插件。当用户单击暂停时,将运行JS脚本,并以单选按钮形式打开jQuery UI对话框(有一些原因)。单击“暂停”将提交用户的选择并杀死窗口。这可以在我的开发网站上完美运行,但是,只能在我的实时网站上偶尔运行。

这是JS的一部分:

$(popUpList).dialog({
            modal: true,
            width:'auto',
            text: "text",
            dialogClass: 'fancybox-container',
            buttons: {
                "Pauzeer": function() {
                    for( i = 0; i < document.pauzeerreden.cancelreason.length; i++ ) {
                            if( document.pauzeerreden.cancelreason[i].checked == true ) {
                                    var val = document.pauzeerreden.cancelreason[i].value;
                                    if(val=='other') {
                                            document.pauzeerreden.cancelreason[i].value=document.pauzeerreden.overig.value;
                                            is_other = 1;
                                    }
                            }
                    }
                        $.ajax({
                      url: ajax_object.ajax_url,
                      type: 'POST',
                      data: {'action': 'wcs_cancel_confirmation', 'subscription_id' : subscription_id, 'reason_to_cancel': $('input[name="cancelreason"]:checked').val(), 'is_other': is_other}
                    });
                        window.location = that.href;
                            $(this).dialog("destroy");
                        },
                "Ik wil toch niet pauzeren" : function() {
                        $(this).dialog("destroy");
                        }
                    }
                });
            return false;
    });

这是发布到的PHP:

    if ( is_account_page() ) {
        wp_register_script( 'wcs-cancel-subscription-confirmation-script', plugin_dir_url( __FILE__ ) . 'wcs-cancel-subscription-confirmation.js', array( 'jquery' ), '1.0.0', true );
        $script_atts = array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
        );
        wp_localize_script( 'wcs-cancel-subscription-confirmation-script', 'ajax_object', $script_atts );
        wp_enqueue_script( 'wcs-cancel-subscription-confirmation-script' );
    }
}
add_action( 'wp_enqueue_scripts', 'wcs_cancel_subscription_confirmation' );


function wcs_cancel_confirmation() {
    $subscription_id = intval( $_POST['subscription_id'] );
    $reason_to_cancel = sanitize_text_field( $_POST['reason_to_cancel'] );
    $subscription = wc_get_order( $subscription_id );
    $email = $subscription->get_billing_email();
    $is_other = $_POST['is_other'];

    $note_id = $subscription->add_order_note( apply_filters( "wcs_cancel_confirmation_note_header", __( "Reden van pauzeren:", "wcs-cancel-confirmation" ) )."<br /><b><i>".$reason_to_cancel."</i></b>" );

    $subscription->save(); }

该代码有效,为什么有时仅在我的实时网站上有效,原因是什么?如果您需要更大一部分的JS代码,请告诉我,我尝试将其尽可能缩小。

它在服务器日志中显示该呼叫未运行的时间:

[Sat Apr 27 18:19:15.040960 2019] [error] [pid 27049] mod_proxy_fcgi.c(860): [client xx.xxx.xxx.xx:62308] AH01071: Got error 'PHP message: -- $action = Array\n(\n    [url] => /mijn-account/view-subscription/111573/?subscription_id=111573&amp;change_subscription_to=on-hold&amp;_wpnonce=ae31aa4954\n    [name] => Pauzeren\n)\n\nPHP message: -- $action = Array\n(\n    [url] => https://fridaymascara.com/mijn-account/?subscription_renewal_early=111573&subscription_renewal=true\n    [name] => Vervroegen\n)\n\n'
[Sat Apr 27 18:19:26.783134 2019] [error] [pid 27049] mod_proxy_fcgi.c(860): [client xx.xxx.xx.xx:62311] AH01071: Got error 'PHP message: -- $action = Array\n(\n    [url] => /mijn-account/view-subscription/111573/?subscription_id=111573&amp;change_subscription_to=active&amp;_wpnonce=da371dce3c\n    [name] => Opnieuw activeren\n)\n\n'

但是,实际运行的呼叫会记录相同的内容。从错误日志来看,关于PHP代码或.htaccess文件的某些信息并不完全正确吗?

1 个答案:

答案 0 :(得分:0)

原因最终是在AJAX调用完成之前重新加载页面!

相关问题