解释 DOM 11 在初始化 XMLHttpRequest 时出现 JSON 标头的错误

Explain DOM 11 error in the initialization of XMLHttpRequest with JSON -headers

本文关键字:JSON 错误 DOM 初始化 XMLHttpRequest 解释      更新时间:2023-09-26

我在这里收到这个错误是这个更大难题的一部分。

var xhr = new XMLHttpRequest();
xhr.setRequestHeader( 'Content-Type', 'application/json' );
//Error: INVALID_STATE_ERR: DOM Exception 11

用于进一步研究

  1. O'Reilly的书"Definite Guide to Javascript 6th Edition"在第491页的第18章"脚本化HTTP"中讨论了XMLHttpRequest,请, 请注意,它不仅与HTTP或XML(历史遗迹)有关。

  2. Mozilla 关于 XMLHttpREquest 的开发条目在这里

您需要先open() XMLHttpRequest,然后才能设置请求标头。只需将该行移动到您呼叫open()之后:

var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'example.php', true );
xhr.setRequestHeader( 'Content-Type', 'application/json' );