突然设置 XMLHttpRequest.responseType 被禁止?

2022-01-15 00:00:00 firefox xmlhttprequest javascript ajax

我一直在使用同步 XMLHttpRequest 并将 responseType 设置为arraybuffer"来加载二进制文件并等待它加载.今天,我收到了这个错误:Die Verwendung des responseType-Attributes von XMLHttpRequest wird im synchronen Modus im window-Kontekt nicht mehr unterstützt."大致翻译为不再支持在 window-context(?) 中以同步模式对 XMLHttpRequest 使用 responseType."

I've been using synchronous XMLHttpRequest with responseType set to "arraybuffer" for quite a while to load a binary file and wait until it is loaded. Today, I got this error: "Die Verwendung des responseType-Attributes von XMLHttpRequest wird im synchronen Modus im window-Kontekt nicht mehr unterstützt." which roughly translates to "Usage of responseType for XMLHttpRequest in synchronous mode in window-context(?) no longer supported."

有谁知道如何解决这个问题?我真的不想对这样的事情使用异步请求.

Does anyone know how to fix this? I realy don't want to use an asynchronous request for something like this.

var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.responseType = 'arraybuffer';

在 chrome 中运行良好.

Works fine in chrome.

推荐答案

这是正确的行为,如 XMLHttpRequest的规范:

This is correct behaviour, as defined in the Specification of XMLHttpRequest:

设置时:抛出 "InvalidAccessError" 异常,如果 同步flag 已设置,并且有一个关联的 XMLHttpRequest 文档.

When set: throws an "InvalidAccessError" exception if the synchronous flag is set and there is an associated XMLHttpRequest document.

XMLHttpRequest非异步即同步时,不能设置responseType属性.将open的第三个参数设置为false会导致请求同步.

The responseType property cannot be set when the XMLHttpRequest is not async, that is, synchronous. Setting the third parameter of open to false causes the request to be synchronous.

相关文章