在我的WordPress REST端点上,我在wordpress上登录以返回一个随机数,如下所示:
.PHP
$authenticated = wp_authenticate( $userID , $password );
$isAuthenticated = ( $authenticated instanceof WP_User ) ? TRUE : FALSE;
if ( $isAuthenticated )
{
$responseData[ "nonce" ] = wp_create_nonce( "rest" );
return rest_ensure_response( $responseData );
}
然后我通过 axios 将随机数返回给 PHP 以验证它并且它有效!
.JS:
let axiosSettings = {
baseURL: "http://site.localhost",
url: "/wp-json/id3/test/verify",
method: "POST",
data: {
n: this.state.nonce
}
}
但是当我把随机数放在标题 X-WP-None 中时,
let axiosSettings = {
baseURL: "http://site.localhost",
url: "/wp-json/id3/test/pc",
method: "POST",
data: {
n: this.state.nonce
},
withCredentials: true,
headers: {
"X-WP-Nonce": this.state.nonce
}
};
它告诉我
Cookie nonce无效,拒绝访问我的REST API。这是为什么呢?
以后会有很多搜索...该操作需要"wp_rest"。
如何使用Wordpress Rest API获取当前登录用户?