chi*_*1ax 4 javascript php rest wordpress reactjs
在我的 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 );
}
Run Code Online (Sandbox Code Playgroud)
然后我通过 axios 将随机数返回给 PHP 来验证它并且它有效!
JS:
let axiosSettings = {
baseURL: "http://site.localhost",
url: "/wp-json/id3/test/verify",
method: "POST",
data: {
n: this.state.nonce
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我将随机数放入标头 X-WP-Nonce 时,
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
}
};
Run Code Online (Sandbox Code Playgroud)
它告诉我
Cookie 随机数无效并拒绝访问我的 REST API。为什么?