我正在使用/wp-admin/admin-post.php
作为端点来接收来自我的Angular应用程序的ajax post请求。这是在我的wordpress插件中注册端点的设置:
function _admin_post_myaction() {
// Just to see what's there
wp_send_json( array(
'$_POST' => $_POST,
'$_GET' => $_GET,
'$_REQUEST' => $_REQUEST
) );
}
add_action( 'admin_post_myaction', '_admin_post_myaction' );
add_action( 'admin_post_nopriv_myaction', '_admin_post_myaction' );
在Angular中,我使用HttpClient发送帖子请求:
this.http
.post(this.config.adminURL + '?action=order_ingredients', {
action: 'order_ingredients',
data: this.data
})
.subscribe({
next: value => {
console.log('Next', value);
},
error: err => {
console.error('Error', err);
},
complete: () => {
console.log('Complete');
}
});
我得到以下结果:
{action: "myaction", $_POST: [], $_REQUEST: {action: "myaction"}
在进入admin-post操作之前,看起来POST数据在某个时刻被WordPress删除了。有任何想法吗?
您需要获取请求的内容,并使用以下内容对json进行解码:
$data = json_decode(file_get_contents('php://input'));