Wordpress AJAX Form



我遇到了最奇怪的问题,我觉得我错过了一些简单的东西。我正在尝试通过Wordpress和AJAX处理一个表单。但当指向正确的url时,它会给我一个400的错误。我有一种感觉,我的动作设置有问题或其他什么。处理函数是同一类中名为process_registration_form的方法。

//JS
$('#user-registration').submit(function(e){
e.preventDefault();
var registrationForm = jQuery(this).serialize();
jQuery.ajax({
action:  'tribe_process_registration_form',
type:    "POST",
url:     tribe_process_user_registration.ajaxurl,
data:    registrationForm,
success: function(data) {
console.log(data);
//jQuery("#feedback").html(data);
}
});
});

//PHP
wp_enqueue_script( 'tribe_process_user_registration', plugin_dir_url( __FILE__ ) . 'js/tribe-product-gifting-public.js', array( 'jquery' ) );
wp_localize_script( 'tribe_process_user_registration', 'tribe_process_user_registration', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action('wp_ajax_tribe_process_user_registration', array($this,'process_registration_form'));
add_action('wp_ajax_nopriv_tribe_process_user_registration', array($this,'process_registration_form'));

我不认为上面的代码有任何问题,但可能是我遗漏了一些东西。下面是我进行ajax调用的方法。

使用wp函数对脚本文件进行入队

wp_enqueue_script( 'pc-frontend-js', plugins_url( 'Scripts/front-end.js', __FILE__ ), false );
wp_localize_script( 'pc-frontend-js', 'pc_var_arguments', array(
'woopb_nonce' => wp_create_nonce('woopb_nonce'),
'ajax_url' => admin_url('admin-ajax.php')
)
);

Ajax调用

function callback_function(quantity) { 
var condition = 'ajax_callback_condition'; 
jQuery.ajax({
url:  pc_var_arguments.ajax_url, 
type : 'post',
dataType: 'json',
data : {
action : 'ajax_callback_action',
condition :condition,
data : data,
},
success : function(response) {
console.log(response);
}   
}); 
};

ajax挂钩

add_action( 'wp_ajax_ajax_callback_action', array($this,'callback_function' ));
add_action( 'wp_ajax_nopriv_ajax_callback_action', array($this,'callback_function' ));

最新更新