If you write WordPress plugins and make AJAX requests, you may be familiar the dreaded Javascript error: SyntaxError: JSON Parse Error: Unrecognized token '>'
Why? Why!?!
What it means is that the response your code expected is screwed up because a plugin barfed PHP warnings into the admin-ajax.php
ventilation system.
When WP_DEBUG
is on, admin-ajax.php
responses can include junk HTML output from PHP warnings, like:
<br /> <b>Notice</b>: Undefined offset: 0 in <b>/wp-content/plugins/im-gonna-break-ur-ajax.php</b> on line <b>666</b><br /> |
The fix? Catch exceptions, then exceed expectations
The way to fix this is to wrap the jQuery.parseJSON()
function in try/catch
. That will make sure that the code doesn’t fully blow up.
try { jQuery.parseJSON( response ); } catch( exception ) { console.log( exception ); } |
That will prevent your code from breaking, but it won’t make your code work.
The cause of the code breaking is the junk HTML at the beginning on the AJAX response. So, what we want to do is:
- Check for a valid JSON response
- If the JSON is invalid, strips all characters until finding a JSON-style opening of
{"
. - Check the newly stripped string to see if that is valid JSON
- If valid, return the JSON. If not, return an error message.
Here’s the code to achieve that:
Let me know what you think, and please fork the Gist if you have any improvements!
You must be logged in to post a comment.