【发布时间】:2021-04-13 13:24:23
【问题描述】:
我正在为 wordpress 开发一个插件,我想在其中使用 ajax。假设我有以下功能:
function ajax_say_hello(){
return "hello";
}
我想将 ajax_say_hello() 的结果作为带有 ajax 的字符串。类似“你好”。
为此,我添加了以下内容:
在我的插件 function.php >
add_action('wp_ajax_ajax_say_hello', 'ajax_say_hello');
add_action( 'wp_ajax_nopriv_ajax_say_hello', 'ajax_say_hello' );
在我的 ajax.js 中 >
jQuery(document).ready(function($) {
$('#myform').on('submit', function(e) {
var data = {
action: 'ajax_say_hello',
};
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
});
并且 ajaxurl 定义正确。
但我得到了整个页面,控制台没有错误。这里出了什么问题,我该怎么办?
【问题讨论】: