在Word preSS Ajax请求返回0 [英] The WordPress Ajax request returns 0
问题描述
我不明白,为什么没有返回
我在阿贾克斯一个真正的初学者..
我刚刚读了很多话题有关Woprd preSS使用Ajax,但例子是非常高级的,我
这是我的JS code combo_checkout_iRange.js
jQuery的(文件)。就绪(函数(){
。jQuery的('#装载机)隐藏();
。jQuery的('#退房更新)隐藏();
jQuery的('#签入最新的')。改变(函数(){
。jQuery的('#退房更新)淡出();
jQuery的('#装载机)显示()。
jQuery.ajax({
键入:POST,
网址:myAjax.ajaxurl,
数据: {
动作:my_ajax_handler,
PARENT_ID:jQuery的(#签入日期)VAL()
},
成功:功能(数据){
警报(数据);
。jQuery的('#装载机)隐藏();
。jQuery的('#退房更新)显示();
。jQuery的('#退房更新)追加(数据);
}});
返回false;
});
jQuery的('#退房更新')。改变(函数(){
警报(jQuery的('#退房更新)VAL());
});
});
这是我写的 function.php
注意:这应该工作在所谓的会议后键入
add_action(wp_enqueue_scripts,函数(){
如果(is_single()){
如果(get_post_type()=='会议)
{
wp_enqueue_script('combo_checkout_iRange',get_template_directory_uri()'/js/combo_checkout_iRange.js',阵列('jQuery的'),'1.0',真的。);
$ data_array中=阵列(
ajaxurl'=> ADMIN_URL(管理-ajax.php)
);
wp_register_script('combo_checkout_iRange',get_template_directory_uri()'/js/combo_checkout_iRange.js',阵列('的jquery'));
wp_localize_script('combo_checkout_iRange','myAjax',$ data_array中);
}
}
});
这是我的AJAX的处理程序,我把它放在 single_meetings.php
add_action(wp_ajax_my_ajax_handler,my_ajax_handler);
add_action(wp_ajax_nopriv_my_ajax_handler,my_ajax_handler);
功能my_ajax_handler(){
如果(使用isset($ _ REQUEST [PARENT_ID])){
的$ id = $ _REQUEST [PARENT_ID];
返回的$ id;
死();
}
}
由于@Nathan说,我的问题是在阿贾克斯处理程序的位置,这样的回调没有回应
I can't figure out why nothing is returned
I am a real beginner in Ajax ..
I just read a lot of topics about using Ajax in Woprdpress but the examples are super advanced for me
Here is my JS code combo_checkout_iRange.js
jQuery(document).ready(function() {
jQuery('#loader').hide();
jQuery('#check-out-date').hide();
jQuery('#check-in-date').change(function(){
jQuery('#check-out-date').fadeOut();
jQuery('#loader').show();
jQuery.ajax({
type: 'POST',
url:myAjax.ajaxurl,
data: {
action : 'my_ajax_handler',
parent_id: jQuery("#check-in-date").val()
},
success: function(data){
alert(data);
jQuery('#loader').hide();
jQuery('#check-out-date').show();
jQuery('#check-out-date').append(data);
}});
return false;
});
jQuery('#check-out-date').change(function(){
alert(jQuery('#check-out-date').val());
});
});
This is what I wrote on function.php
Note: this should work in post type called "meetings"
add_action("wp_enqueue_scripts", function() {
if (is_single()) {
if (get_post_type() == 'meetings')
{
wp_enqueue_script('combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array( 'jquery' ), '1.0' ,true);
$data_array = array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
);
wp_register_script( 'combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array('jquery') );
wp_localize_script( 'combo_checkout_iRange', 'myAjax', $data_array );
}
}
});
and this is my ajax handler i put it inside single_meetings.php
add_action("wp_ajax_my_ajax_handler", "my_ajax_handler");
add_action("wp_ajax_nopriv_my_ajax_handler", "my_ajax_handler");
function my_ajax_handler() {
if ( isset($_REQUEST["parent_id"]) ) {
$id = $_REQUEST["parent_id"];
return $id;
die();
}
}
As @Nathan said, my problem was in the location of the Ajax handler so the callback didn't respond
这篇关于在Word preSS Ajax请求返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!