admin管理员组文章数量:1278690
I try to add custom form in my child-theme. I have been read all documentation of this issues but still return 0, this my code
function.php (child-theme)
function my_custom_scripts() {
wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/assets/js/custom-child.js', array( 'jquery' ),'',true );
//the_ajax_script will use to print admin-ajaxurl in custom ajax.js
wp_localize_script('custom-js', 'the_ajax_script', array('ajaxurl' =>admin_url('admin-ajax.php')));
}
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );
add_action( 'wp_ajax_callbackcity', 'callbackcity' );
add_action( 'wp_ajax_nopriv_callbackcity', 'callbackcity' );
function callbackcity() {
extract($_POST);
$data = 'tes city - '.$svalue;
return $data;
wp_die();
//die();
}
custom-child.js
jQuery("#get_city").on("change", function (e) {
e.preventDefault();
jQuery.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: {
'action': 'callbackcity',
'svalue': 12
},
success: function(data) {
console.log(data);
},
error: function() {
console.log('ERROR');
}
});
});
can anyone tell me what's wrong with my code?
I try to add custom form in my child-theme. I have been read all documentation of this issues but still return 0, this my code
function.php (child-theme)
function my_custom_scripts() {
wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/assets/js/custom-child.js', array( 'jquery' ),'',true );
//the_ajax_script will use to print admin-ajaxurl in custom ajax.js
wp_localize_script('custom-js', 'the_ajax_script', array('ajaxurl' =>admin_url('admin-ajax.php')));
}
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );
add_action( 'wp_ajax_callbackcity', 'callbackcity' );
add_action( 'wp_ajax_nopriv_callbackcity', 'callbackcity' );
function callbackcity() {
extract($_POST);
$data = 'tes city - '.$svalue;
return $data;
wp_die();
//die();
}
custom-child.js
jQuery("#get_city").on("change", function (e) {
e.preventDefault();
jQuery.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: {
'action': 'callbackcity',
'svalue': 12
},
success: function(data) {
console.log(data);
},
error: function() {
console.log('ERROR');
}
});
});
can anyone tell me what's wrong with my code?
Share Improve this question asked Sep 27, 2021 at 5:11 raja arabraja arab 1 1 |1 Answer
Reset to default 03 major problems:
extract
, is dangerous and introduces major security issues. It's also completely unnecessaryreturn
instead ofwp_die
, you're meant to echo the output and then exit/die. If you do not then WP will continue to handle the AJAX request, assume no handler was found, and print0
- JSON data type, the old legacy
admin-ajax.php
wasn't built to use that data type, and requires complicated work arounds for it
Consider using a newer modern REST API handler instead. It will give you human readable messages, works closer to what you expected, has a pretty URL e.g. /wp-json/raja/v1/callbackcity
, and supports JSON data type requests.
本文标签: child themeAjax call returning 0 in page template
版权声明:本文标题:child theme - Ajax call returning 0 in page template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741299691a2371021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
extract
as it's dangerous, is there a rerason you went with the old Admin AJAX api instead of the new/modern REST API? The rest API will tell you in readable text what the problem was, admin-ajax wil just say0
. Additionally, I noticed you chose to use thedataType: 'json',
data type to make your request, this is extremely unusual when usingadmin-ajax.php
, and unsupported. To use the JSON data type will require some workarounds. If you used the REST API instead it would be supported out of the box. I also see the code is returning, not exiting, this is not normal – Tom J Nowell ♦ Commented Sep 27, 2021 at 10:18