Created
May 18, 2018 07:14
-
-
Save Komock/8e15e4be228a92f6ff7529ba33d84254 to your computer and use it in GitHub Desktop.
Submit form with wp-nonce token using fetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const formEl = document.querySelector('.form-profile-upd'); | |
| formEl.addEventListener('submit', e => { | |
| e.preventDefault(); | |
| const data = new FormData(formEl); | |
| const id = window.WP_DATA.USER_ID; | |
| console.log(data.get('birthday')); | |
| fetch(`${WP_DATA.ROOT_URL}wp/v2/users/` + id, { | |
| method: 'POST', | |
| credentials: 'same-origin', | |
| body: { | |
| 'meta': { | |
| 'birthday': data.get('birthday') | |
| } | |
| }, | |
| headers: { | |
| 'X-WP-Nonce': WP_DATA.WP_NONCE | |
| } | |
| }) | |
| .then(res => res.text()) | |
| .then(data => console.log(data)); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment