中止来自 Web Worker 的 ajax 调用
Abort ajax call from web worker
我今天阅读了一些关于 Web Workers 的文章,并尝试了一些关于 ajax 请求的内容。事实上,我希望能够存储我的网络工作者发送的每个 ajax 请求,以便在您单击某个元素时中止它们。
但是,XHR 对象无法被克隆,因此无法通过 postMessage() worker 方法发送。
我试过这样做:
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
let DONE = 4; // readyState 4 means the request is done.
let OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
handlePoints(JSON.parse(xhr.responseText));
}
}
xhr.open('GET', '/api/widgets/burndownchart?widgetId='+ widgetId);
xhr.send(null);
self.postMessage([constants.EVENT_AJAX_CALL, xhr]);
当然我有这个错误:
Uncaught DOMException: Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope': XMLHttpRequest object could not be cloned.
你知道怎么做吗?
PS : 抱歉我的语言错误。
您需要将对象保留在 Web Worker 中并向 Web Worker 发送一条消息,要求它按需中止请求。
我今天阅读了一些关于 Web Workers 的文章,并尝试了一些关于 ajax 请求的内容。事实上,我希望能够存储我的网络工作者发送的每个 ajax 请求,以便在您单击某个元素时中止它们。 但是,XHR 对象无法被克隆,因此无法通过 postMessage() worker 方法发送。
我试过这样做:
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
let DONE = 4; // readyState 4 means the request is done.
let OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
handlePoints(JSON.parse(xhr.responseText));
}
}
xhr.open('GET', '/api/widgets/burndownchart?widgetId='+ widgetId);
xhr.send(null);
self.postMessage([constants.EVENT_AJAX_CALL, xhr]);
当然我有这个错误:
Uncaught DOMException: Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope': XMLHttpRequest object could not be cloned.
你知道怎么做吗? PS : 抱歉我的语言错误。
您需要将对象保留在 Web Worker 中并向 Web Worker 发送一条消息,要求它按需中止请求。