1前端页面
<script type="text/javascript"> jQuery.ajax({ type: ‘POST‘, url: "<?php echo admin_url( ‘admin-ajax.php‘ );?>", data: { action: ‘myajax_submit‘ }, success: function(res) { console.log(JSON.stringify("--"+res)); } }); </script>
2 后端functions.php里面接受请求
add_action( ‘wp_ajax_nopriv_myajax_submit‘, ‘myajax_submit‘ ); add_action( ‘wp_ajax_myajax_submit‘, ‘myajax_submit‘ ); function myajax_submit() { $response = json_encode( array( ‘success‘ => true ) ); header( "Content-Type: application/json" ); echo $response; // 这个很关键啊有木有: 别忘记 "exit" exit; }
注意 前端action名字要和后端wp_ajax_nopriv_XXX名字后缀对应起来
剩下的按照格式相应改就可以了,
wp_ajax_nopriv_
时间: 2024-10-13 21:48:45