The problem is that you’re not using the correct name for the REST API nonce header — the correct name is X-WP-Nonce
, but you used X-WP-Header
:
fetch('http://localhost/wptest2/?rest_route=/wp/v2/users/me', {
method : 'get',
mode : 'cors',
headers : {
'Access-Control-Allow-Origin' : '*',
'X-WP-Header' : _wpnonce // here you used the wrong name
}
})
Reference from the REST API Handbook:
For developers making manual Ajax requests, the nonce will need to be
passed with each request. The API uses nonces with the action set to
wp_rest
. These can then be passed to the API via the_wpnonce
data
parameter (either POST data or in the query for GET requests), or via
theX-WP-Nonce
header.
So make sure to use the correct header name. 🙂
Related Posts:
- Authentication with the Rest API when using an External Application
- How to delete a cookie?
- nodejs – error self signed certificate in certificate chain
- javascript set cookie with expire time
- display variable image using cookies
- jQuery $.cookie is not a function
- How do I set/unset a cookie with jQuery?
- Make Axios send cookies in its requests automatically
- How do I create and read a value from cookie?
- How do I remove documents using Node.js Mongoose?
- Get cookie by name
- How do I check if a cookie exists?
- Nonce retrieved from the REST API is invalid and different from nonce generated in wp_localize_script
- REST API: Backbone and custom endpoint
- Gutenberg custom block plugin with custom image sizes
- Get loading state of wp data selector
- How to build a plugin that supports authenticated POST requests to the REST API from external servers?
- WP REST – video and audio players
- Headless WordPress: How to authenticate front end requests?
- Post to WordPress using REST API from external site
- How to handle malformed response from WP REST API?
- Get HTTP response code on non-2xx apiFetch request
- Can’t GET draft posts via REST API from headless frontend
- Rest API invalid nonce with Backbone Client
- Logout user and delete cookies from the client side?
- WP REST API Post Status Using JavaScript
- How to transform a legacy widget into a block
- How to save generated JWT token to cookies on login?
- Get Block Attributes in JSON REST API
- Setting/unsetting terms using the Backbone JavaScript client
- Checks when fetching data from multiple REST API endpoints in Gutenberg
- Search for a keyword across post types in a Gutenberg component
- Using apiFetch for retrieving post data in Gutenberg
- How to generate the COOKIEHASH from JavaScript
- Get terms of a taxonomy using useSelect
- Return ‘X-WP-Total’ from headers in response
- How to render WP Rest-API Endpoints in a React.js Theme with Woocommerce
- Retrieving data about comments and likes
- how to use nimble-API and Display data?
- Log in user using WordPress REST API
- How to verify which WordPress user requested the API in ASP .NET Core?
- Check if user can in javascript
- wp-api Backbone JS Client fetch options
- Why does my array sort order changes when I pass it to JS using WP_REST_Response?
- Rest API and how to deal with it in server side
- Access checks with custom REST endpoints and backbone
- Console Messages: A cookie associated with a cross-site resource at
- The same session information for peer users on two different WordPress servers
- [Vue warn]: Error in render: “TypeError: Cannot read property ‘wp:featuredmedia’ of undefined – REST API
- Update block once an API request returns with a value
- Get user in rest API endpoint
- How to improve WP-Rest atrocious response time?
- How are cookie values encoded?
- I would like to retrive JSON value and display it in wordpress page or widget
- Connecting a wordpress site to an AngularJS APP
- WordPress REST API response is empty in browser and script, but not in Postman
- WP REST API – “rest_user_cannot_view” ONLY on specific users
- Rest Api WordPress
- Get Comment Text via REST API
- Embedded data and the _fields parameter in getEntityRecords
- REST API not returning all tags – is there a way to return all tags even ones that are not part of published posts?
- Get a default customizeAction text for a section using Customizer JS API
- Dynamically write in editor with Javascript
- Redirect to homepage if attmpting to leave intranet [closed]
- Taxinomy slug as var in parent archive page, without having a specific taxinomy page
- Twenty seventeen theme dropdown menu issue
- How to verify that a field has a value before adding a term to a taxonomy
- Including js script at end of header
- Where is fusion-gmap-container?
- Dynamically changing logo on scroll function [closed]
- Escaping quotes while enqueuing scripts
- Loading posts outside wordpress
- The page isn’t redirecting properly was given when redirecting
- Cookie is not set
- How To Pull Information From A Page And Input The Data In The WordPress User Database Table
- How to register or enqueue script and stop it being called in head?
- Notify user we would like them to clear their cookie. Provide a working button to clear cookies for user
- How to correctly load this jquery script through the file functions.php?
- contact form 7 javascript and wpcf7_before_send_mail action
- Adding javascript script to header via functions.php
- Variations Javascript not working when product display on Quick view via ajax
- adding script tag in head of specific pages
- How to use value from modal tinymce windowManager?
- how to refresh page after user logout with ‘wp_logout_url’?
- Open image gallery on link click
- How do I call for two js files into a custom template?
- How to delay display of page elements until enqueued script has injected html
- load-scripts.php loads incorrect file names
- How to change number field to text field using JS
- Enqueue concatinated JS file in WordPress
- Custom Media Meta fields to alter the generated image HTML that is inserted into a post for my animated GIF image player?
- wp_enqueue_scripts doesn’t work for template pages
- WordPress text widget with onclick function
- Create new product with woocommerce REST API with javascript (clientside)?
- Can’t change javascript files
- how to do open a link in a open window from menu bar? [closed]
- Combining results from WP-API using AngularJS
- Logged In cookie gets saved but not Auth cookie
- JavaScript and Google PageSpeed + wp_enqueue_script
- Javascript file doesn’t load