我遇到了一个问题,我无法找到原因,我试图打印变量并检查拼写错误,但是找不到任何内容。
我向Ajax发送了Ajax请求:
function update_order() {
var orderdata = {
action: 'update_order_ajax',
data: 'test'
};
jQuery.ajax({
type: 'POST',
beforeSend: function (jqXHR) {
if (currAjax != null) {
currAjax.abort();
}
currAjax = jqXHR;
},
url: ajaxurl,
data: orderdata,
dataType: 'text',
success: function (response) {
alert(response);
}
}); //end Ajax
} //end function
//Run the above function on submitting a form.
jQuery('#updateOrder').on('submit', function(event) {
event.preventDefault();
update_order();
});
文件位于插件文件夹“ http://localhost/test/wordpress/wp-content/plugins/update-order/template”内的文件夹中
在该文件夹中,我有:
function update_order_ajax() {
echo $_POST['data']; //That should return 'test'
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_update_order_ajax', 'update_order_ajax' );
但是我得到了
POST http://localhost/test/wordpress/wp-admin/admin-ajax.php 400 (Bad Request)
我搜索并查看了许多相关问题,并尝试为admin-ajax.php
使用自定义URL,还检查了错误文件,但找不到任何解决方案。
我检查了admin-ajax.php
,发现它返回400
,何时:
$_REQUEST['action']
为空。! has_action( 'wp_ajax_' . $_REQUEST['action'] )
! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] )
出什么问题了?