After some fiddling and sparring with @t-todua I found the issue:
With the Fetch API fetch
call you must manually set to send cookies with a request. After setting the credentials
option properly the cookies were sent and the AJAX endpoint recognized the current user. So the JS becomes:
var msg = '';
// I'm using the whatwg-fetch polyfill and a polyfill for Promises.
fetch(ajaxurl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
body: 'action=foobar_action&_wpnonce=" + window.custom_nonce,
credentials: "same-origin'
}).then(function (res) {
msg = res.json().msg;
});
Related Posts:
- ajax and nonce when JavaScript is in a seperate file
- AJAX requests broken due to HTTPS for wp-admin
- Nonces, AJAX, script variables & security in WordPress
- how to send Ajax request in wordpress backend
- WP Admin AJAX Security – using POST to include a relative URL
- SSO autologin WordPress + Ajax
- javascript ajax and nonce
- How to check nonce lifetime value of plugins?
- AJAX form not working, still reloads on submit
- Is it safe to assume that a nonce may be validated more than once?
- Ajax for non-logged-in users
- Using Nonces for AJAX that only retrieves data
- How to verify nonce from Bulk/Quick Edit in save_post?
- How to add WordPress nonces to ajax request
- Refused to execute script from ‘***’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled
- Ajax form submission from admin panel
- Dynamically changing navigation links (next and previous) via AJAX
- Ajax function returns -1
- Quick Edit: Selected Custom Taxonomy Not Refreshing After Save
- wp_verify_nonce always returns false when logged in as admin
- fetching via fetch/ajax gutenberg block data from third party
- Elegantly using JavaScript on widget admin forms
- admin-ajax.php HTTP400: BAD REQUEST – The request could not be processed by the server due to invalid syntax
- WordPress AJAX Call Not Return Result
- WP-API and Basic Auth returning 403 on POST but not GET
- Extending wp JavaScript base class to make a post request to a custom REST endpoint
- Why does WordPress Heartbeat login not refresh the nonces?
- How to localized one js file for different actions?
- How do I check if AJAX nonces are implemented correctly?
- Admin Ajax and HTML5 Formdata
- How to check an ajax nonce in PHP
- Help with AJAX front end comment moderation
- Can a wp_nonce created from domain 1 to be verified on domain 2?
- Woocommerce Ajax Add cart not working
- Identical wp_rest nonce returned from rest_api
- Ajax Modal Flickers When Opened Multiple Times
- wp_create_nonce() in REST API makes user->ID zero
- ajax nonce verification failing
- WordPress Ajax Not Working ( Custom Admin page)
- Load JavaScript from a post that’s loading into Fancybox via ajax
- splitting the URL using jQuery
- AJAX Load more on CPT returning random posts
- Pass additional parameter with async upload
- Refresh Markercluster after ajax call
- Making an ajax request from a different domain
- Performance optimization of tree like structure
- How do I query posts by a sub value with the API?
- Radio buttons live refresh in the customizer
- Gravity Forms closes my popup on Validation Error [closed]
- Why is wp_localize_script returning false?
- Cache plugins and ajax nonce verification
- Nonce doesn’t validate in nopriv call
- Using admin-ajax prevents regular php form submission
- AJAX wp_insert_user WORKS but responds with “The site is not enabled”
- Why does check_ajax_referer give a 403 error on https websites?
- WordPress is creating nonce as a logged in user but verifying it incorrectly
- Preprocess submitted data
- wp_mail doesn’t work when logged in?
- Hide Load more Ajax button if there is no more users to load or less than the number?
- How to disable drag-and-drop upload function in Media Library?
- Using nonce when loading posts with AJAX
- What WP-API authentication method should I use to interact with anonymous / not-logged visitors?
- Run javascript upon successfully set featured image
- admin-ajax.php (aborted) error when using jQuery.get
- Replace link with form to pass variables to javascript / ajax
- Load page HTML content through AJAX
- admin-ajax.php returns 0 even when the post status code is 200 OK
- How to display contact form 7 form in vanilla js without jquery in frontend
- Get localize of a loaded javascript
- Refresh Gutenberg with JS for it show updated post
- javascript onClick update user_meta from jquery.ajax
- Can I use application/json content type in WordPress
- How to Object.freeze wp_localize_script
- How to pass value from ajax to php in no conflict mode?
- Ajax sometimes work and sometimes just don’t work
- wp_verify_nonce not working on the mobile device
- How do I mitigate replay attacks when talking about actions that shouldn’t happen twice?
- React to AJAX adding to the page
- wordpress admin ajax trash_comment
- Javascript output now showing in custom widget
- Ajax call from Plugin using Class
- Ajax call not working anymore
- Ajax call on class returns old data
- How to safely pass post_id and user_id via AJAX to the backend (prevent user from changing it via JS)?
- Ajax show custom post data form & script
- Create a post with REST API and adding a category
- Jquery wrap permalink in a data-attribute?
- admin-ajax.php & my wp-admin folder url showing in header
- How modify comments metabox on post edit screen in WordPress?
- WordPress Get Header and Footer using in Admin Area
- get_comments() returns empty array if called through AJAX
- Usage of wp_send_json_success and wp_redirect at the same time
- ‘Dehighlighting’ navigation once clicked
- AJAX admin Internal 500 error Failed to Upload
- Using Javascript Callback from plugin in a theme
- WordPress wp_localize_script nonce and ajax URL
- Running js in html code with same content
- How to get current_user_id from wordpress in node js?
- AJAX call not initializing for non-admins in WordPress
- randomly get 400 error while user is logged in wp_ajax