【发布时间】:2020-12-25 03:56:24
【问题描述】:
您好,您可以看到我正在尝试使用 ajax post call 将我的数据发送到 admin-ajax.php 请检查下面的代码,我使用的模板是 betheme。
index.html
$('#send-form').on('submit', function () {
$.ajax({
url : '<?php echo admin_url('admin-ajax.php'); ?>',
data : {
'action' : 'test_func',
'data':'lorem ipsum'
},
type : 'POST',
contentType: "application/json; charset=utf-8",
dataType : 'json',
success : function (callback) {
console.log(callback);
}
});
return false;
});
functions.php
function test_func () {
echo json_encode(array('res'=>'return dummy text'));
}
add_action('wp_ajax_nopriv_test_func', 'test_func');
【问题讨论】:
-
使用 wp_send_json() 而不是 echo - 这就是“WP 方式”codex.wordpress.org/Function_Reference/wp_send_json
-
嘿@muka.gergely 好消息欢呼
标签: wordpress