运行 XMLHttpRequest 完成后的方法
Running then method after XMLHttpRequest done
我有一个类似于异步方法的方法。在请求发送到调用此请求的函数后,我想 运行 类似 then 方法的东西,但是 XMLHttpRequest 没有 then 方法。
下面代码中的调用函数没有 then 方法
let result = dataService.exportfile('get', '/api/overtimeedari/exporttoexcle/', model).
then(() => {
self.loading(false);//غیرفعال کردن حالت لود شدن گرید
buttonListSearch.Excel.loading(false); //غیرفعال کردن حالت لود شدن دکمه اکسل
});
调用的函数
function exportfile(mehtodtype, url, model) {
debugger;
var qs = "?";
model.map((item) => {
qs = `${qs}${item.name}=${item.value}&`;
});
var request = new XMLHttpRequest();
request.open(mehtodtype, url + qs, true);
request.setRequestHeader('Authorization', "Bearer " + window.localStorage.getItem('token'));
request.responseType = 'blob';
request.onload = function (e) {
if (this.status === 200) {
var blob = this.response;
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, fileName);
}
else {
var downloadLink = window.document.createElement('a');
var contentTypeHeader = request.getResponseHeader("Content-Type");
downloadLink.href = window.URL.createObjectURL(new Blob([blob], { type: contentTypeHeader }));
downloadLink.download = "Export.xls";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
};
request.send();
return request;
}
考虑到不根据评论更改 exportfile
函数的约束
the case is I don't have this ability to change theexportfile
function because it has side affects on other functions
最好的处理方法如下
let req = dataService.exportfile('get', '/api/overtimeedari/exporttoexcle/', model);
req.addEventListener('loadend', () => {
// do what's needed here
});
由于 exportfile
returns XMLHttpRequest 对象,您可以侦听 loadend
事件并在那里执行任何操作
注意,loadend
事件无论成功还是失败都会触发
如果您也愿意,可以对 load
事件执行上述操作 - 但是,我不确定顺序是什么
x.onload=() => {};
x.addEventListener('load', () => {});
被解雇了...另请注意,不要
req.onload=() => {};
因为这会覆盖函数内的 onload 回调
我有一个类似于异步方法的方法。在请求发送到调用此请求的函数后,我想 运行 类似 then 方法的东西,但是 XMLHttpRequest 没有 then 方法。 下面代码中的调用函数没有 then 方法
let result = dataService.exportfile('get', '/api/overtimeedari/exporttoexcle/', model).
then(() => {
self.loading(false);//غیرفعال کردن حالت لود شدن گرید
buttonListSearch.Excel.loading(false); //غیرفعال کردن حالت لود شدن دکمه اکسل
});
调用的函数
function exportfile(mehtodtype, url, model) {
debugger;
var qs = "?";
model.map((item) => {
qs = `${qs}${item.name}=${item.value}&`;
});
var request = new XMLHttpRequest();
request.open(mehtodtype, url + qs, true);
request.setRequestHeader('Authorization', "Bearer " + window.localStorage.getItem('token'));
request.responseType = 'blob';
request.onload = function (e) {
if (this.status === 200) {
var blob = this.response;
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, fileName);
}
else {
var downloadLink = window.document.createElement('a');
var contentTypeHeader = request.getResponseHeader("Content-Type");
downloadLink.href = window.URL.createObjectURL(new Blob([blob], { type: contentTypeHeader }));
downloadLink.download = "Export.xls";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
};
request.send();
return request;
}
考虑到不根据评论更改 exportfile
函数的约束
the case is I don't have this ability to change the
exportfile
function because it has side affects on other functions
最好的处理方法如下
let req = dataService.exportfile('get', '/api/overtimeedari/exporttoexcle/', model);
req.addEventListener('loadend', () => {
// do what's needed here
});
由于 exportfile
returns XMLHttpRequest 对象,您可以侦听 loadend
事件并在那里执行任何操作
注意,loadend
事件无论成功还是失败都会触发
如果您也愿意,可以对 load
事件执行上述操作 - 但是,我不确定顺序是什么
x.onload=() => {};
x.addEventListener('load', () => {});
被解雇了...另请注意,不要
req.onload=() => {};
因为这会覆盖函数内的 onload 回调