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_wpnoncedata
parameter (either POST data or in the query for GET requests), or via
theX-WP-Nonceheader.
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
- Google reCAPTCHA V3.0 not working. “ERROR for site owner: Invalid key type”
- Javascript split regex question
- Find object by id in an array of JavaScript objects
- How fix ‘Failed to load resource: the server responded with a status of 403’?
- How do I empty an array in JavaScript?
- JavaScript TypeError: Cannot read property ‘style’ of null
- Usage of the backtick character (`) in JavaScript
- Why is using “for…in” for array iteration a bad idea?
- Parsing a string to a date in JavaScript
- Number converted in 1e+30
- XML Parsing Error: no root element found Location in Console FF
- “.addEventListener is not a function” why does this error occur?
- Obtain form input fields using jQuery?
- Open URL in new window with JavaScript
- Read XML file using javascript
- Load Image from javascript
- Most efficient method to groupby on an array of objects
- Don’t make functions within a loop [duplicate]
- How to change the text of a label?
- How to check ‘undefined’ value in jQuery
- Detecting arrow key presses in JavaScript
- Data column(s) for axis #0 cannot be of type string in google chart dashboard [duplicate]
- JavaScript – Count Number of Visitor for Website
- Jquery Value match Regex
- clearInterval() not working [duplicate]
- How to make HTML element resizable using pure Javascript?
- how to use addHTML function in jsPDF
- Rock, Paper, Scissors, Lizard, Spock in JavaScript
- How to fix “TypeError: Right-hand side of ‘instanceof’ is not callable” when I use another module class?
- Change the selected value of a drop-down list with jQuery
- Uncaught SyntaxError: Unexpected token var
- window.location.href doesn’t redirect
- Custom “Uploads” Dir: “Edit Image” in Media Library broken
- Gutenberg RichText
- Configuring WordPress Auth Cookie Expiration
- Unwanted line break before
- Weird links found in wordpress footer
- How to securely set dynamic HTML content with JavaScript?
- WordPress Network / Multisite login to one site allow access to all
- Processing javascript on wordpress
- How to get selected category objects in the block editor?