作为这个更大难题的一部分,我收到此错误 here .
var xhr = new XMLHttpRequest();
xhr.setRequestHeader( 'Content-Type', 'application/json' );
//Error: INVALID_STATE_ERR: DOM Exception 11
进一步研究
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
请您参考如下方法:
在设置请求 header 之前,您需要open()
XMLHttpRequest
。在调用 open()
之后,只需将该行移动到:
var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'example.php', true );
xhr.setRequestHeader( 'Content-Type', 'application/json' );