Estoy tratando de establecer el tiempo de espera en XMLHttpRequest pero muestra invalid state error , aquí está el código
function get(url, options) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); // headers if (options && options.headers) { for (let header in options.headers) { if (options.headers.hasOwnProperty(header)) { xhr.setRequestHeader(header, options.headers[header]); } } } xhr.open('GET', url); // FIXME: Why is IE11 failing on "xhr.timeout? // xhr.timeout = 10000; xhr.onload = function () { if (this.status >= 200 && this.status < 300) { try { const data = JSON.parse(xhr.responseText); resolve(data); } catch (ex) { reject({ status: this.status, statusText: xhr.statusText }); } } else { reject({ status: this.status, statusText: xhr.statusText }); } }; xhr.ontimeout = function () { reject({ status: this.status, statusText: xhr.statusText }); }; xhr.onerror = function () { reject({ status: this.status, statusText: xhr.statusText }); }; xhr.send(); }); } export default { get }; Eché un vistazo a los siguientes enlaces link1 link2link3 y específicamente mantuve xhr.timeout entre xhr.open y xhr.send
Incluso probé esto
xhr.onreadystatechange = function () { if(xhr.readyState == 1 ) { xhr.timeout = 5000; } };pero sin suerte
Agregue xhr.timeout después del método xhr.open .
Debe configurar xhr.timeout solo cuando llame a xhr.open() en modo asincrónico ; de lo contrario, MSIE generará una excepción INVALID_STATE_ERR .
Ejemplo: xhr.open("POST", url, true);
PD. Extrañamente, no veo este problema en FF y Chrome.