WordFence corrupts JSON response from jQuery ajax if not 200
-
I had previously raised the issue here, but it was not resolved. Now it’s annoying to deal with the issue which is not under my control as a plugin developer.
Steps to reproduce:
1. Open the console in wp-admin with Wordfence active.
2. Run this code which results in an error (404), because of v1 instead of v2jQuery.ajax({ type: 'GET', url: location.origin + '/wp-json/wp/v1/posts', contentType: 'application/json; charset=utf-8', dataType: 'json', }) .then(data => console.log(data)) .fail(jq => console.log(jq));
3. Although the error handler
.fail()
is defined, you will still see the syntax errorUncaught Error: Syntax error, unrecognized expression: {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
Now if you deactivate Wordfence, the request error will gracefully be handled.
After thorough debugging, I found that
wordfenceAJAXWatcher
watcher is the culprit. If you comment out this line, the error is gone.I dug even further to see that if you comment out this line, it goes away.
I could not dig further as the code in the JS file is minified.
- The topic ‘WordFence corrupts JSON response from jQuery ajax if not 200’ is closed to new replies.