Nonce being receieved by endpoint but failing check
-
Can anyone spot the error in this code please?
custom-page.php:
<form name="customForm"> <?php wp_nonce_field('code_check', 'codecheck'); ?> Validation Code:<br> <input type="password" name="inputcode" id="inputcode" maxlength="6" inputmode="numeric"> <input type="text" name="message" id="message" style="display:none; background-color: #FFCCCC;"><br> <input type="button" name="submitbutton" value="Submit" onClick="customfunction()"> </form>custom.js:
function customfunction() { const userInput = document.addStamp.inputcode.value; const token = document.addStamp.codecheck.value; fetch(<code>http://...../wp-json/api/v1/custom?code=${userInput}&token=${token}</code>).then(r => r.json()).then(data => { ......API file.php:
public function custom($request) { $params = $request->get_params(); $retrieved_nonce = $params[token]; if($retrieved_nonce) { if (!wp_verify_nonce($retrieved_nonce, 'code_check' ) ) die( 'Failed security check' ); } ....Everything works fine until I added in the nonce verify code to the api request.
Now when I click on “submit” button, it does not submit and I get in console:
Uncaught (in promise) SyntaxError: Unexpected token F in JSON at position 0So it is failing as “F” is point 0 of the failure message.
However, if I output “$retrieved_nonce” I actually get the nonce value as shown in my page source code, so it looks like it is getting to the endpoint?
I have tried logging out and back in but no change.
Do I have this code set up wrong?
The topic ‘Nonce being receieved by endpoint but failing check’ is closed to new replies.