I am getting this error as part of this larger puzzle here.
var xhr = new XMLHttpRequest();
xhr.setRequestHeader( 'Content-Type', 'application/json' );
//Error: INVALID_STATE_ERR: DOM Exception 11
For further research
O'Reilly's book "Definite Guide to Javascript 6th Edition" on page 491 in chapter 18 "Scripted HTTP" discussed XMLHttpRequest, please, note that it is not only about HTTP or XML (historical relics).
Mozilla's dev entry about XMLHttpREquest here
The solution to the above problem is to set async setting of the jQuery AJAX function to true as AJAX itself means Asynchronous JavaScript and XML and hence if you make it Synchronous by setting async setting to false, it will no longer be an AJAX call.
You can solve the "Unexpected end of JSON input" error in the following 3 ways: wrap your parsing logic in a try/catch block. make sure to return a valid JSON response from your server. remove the parsing logic from your code if you are expecting an empty server response.
The only reliable way to check if an arbitrary string is json or not in JS is to try and parse it, and catch the error. Also, the content-type won't tell you if it is valid json.
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
You need to open()
the XMLHttpRequest
before you can set request headers. Just move that line to after you call open()
:
var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'example.php', true );
xhr.setRequestHeader( 'Content-Type', 'application/json' );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With