I am trying to get the login details of my wordpress account via WP Rest-Api with Cookies Auth in the logged-in wordpress condition. However, when I run it through a web browser I do not get anything with the http 200 response. I expect to get something on my console. Maybe something is wrong with my code? or I don't get authorization cookies?
This is my PHP script
<?php
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', '/var/www/html/mine/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-config.php');
do_action( 'plugins_loaded' );
add_action( 'set_logged_in_cookie', 'my_update_cookie' );
function my_update_cookie( $logged_in_cookie ){
$_COOKIE[LOGGED_IN_COOKIE] = $logged_in_cookie;
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
function my_enqueue_scripts() {
// Enqueue the script which makes the AJAX call to /wp-json/my-plugin/v1/foo.
wp_enqueue_script( 'wp-api', 'my-script.js', [ 'jquery' ] );
// Register custom variables for the AJAX script.
wp_localize_script( 'wp-api', 'wpApiSettings', array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
}
?>
<!doctype html>
<html lang="en">
<head>
<script src=".11.1/jquery.min.js"></script>
</head>
<body>
</body>
</html>
This is my Ajax script
$.ajax( {
url: wpApiSettings.root + 'wp/v2/users/me',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
console.log( response );
} );
Please help me Master
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742299128a4417707.html
评论列表(0条)