[英]Ajax function call to php function
I have a simple php function as below defined in one of my php file我有一个简单的 php function 在我的 php 文件之一中定义如下
add_action('wp_ajax_test','test');
add_action('wp_ajax_nopriv_test','test');
function test(){
echo "Hello";
}
I am calling above "test" function using below ajax function call我在上面调用“测试”function 使用下面的 ajax function 调用
jQuery(document).ready(function(){
jQuery('#prev_button_id').click(function(){
jQuery.post(
myAjax.ajaxurl,
{
action:"test",
success: (response)=>{
console.log('Success:'+ response);
},
error:(response)=>{
console.log('Failure:'+ response);
}
});
});
});
I am expecting console output of "Hello" but I getting below undefined response我期待“Hello”的控制台 output 但我得到低于未定义的响应
Success:undefined
Failure:undefined
jQuery(document).ready(function(){
jQuery('#prev_button_id').click(function(){
var params = {action:"test"}
jQuery.post(myAjax.ajaxurl,params,function(data){
if( data ){
console.log ( data );
}
});
});
});
add_action( 'wp_ajax_test','test' );
add_action( 'wp_ajax_nopriv_test','test' );
function test() {
echo "Hello";
die;
}
Try this code and don't forget to die
after AJAX call also make sure you are getting proper AJAX URL in myAjax.ajaxurl
试试这个代码,不要忘记在 AJAX 调用之后die
,还要确保你在 myAjax.ajaxurl 中得到正确的 AJAX myAjax.ajaxurl
I followed the below video, step by step and it worked.我按照下面的视频一步一步地进行操作,并且成功了。 Probably it had something to do with nonce AJAX in WordPress using admin-ajax.php可能它与使用 admin-ajax.php 的 WordPress 中的 nonce AJAX 有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.