简体   繁体   English

在 XMLHttpRequest 完成后运行 then 方法

[英]Running then method after XMLHttpRequest done

I have a method acting like an async method.我有一个像异步方法一样的方法。 After the request sends to the function that was called this request, I want to run something like the then method but then there is no then method for XMLHttpRequest.在请求发送到调用此请求的函数后,我想运行类似 then 方法的内容,但随后没有用于 XMLHttpRequest 的 then 方法。 the caller function in below code has no then method下面代码中的调用函数没有 then 方法

      let result = dataService.exportfile('get', '/api/overtimeedari/exporttoexcle/', model).
                then(() => {
                        self.loading(false);//غیرفعال کردن حالت لود  شدن گرید
                        buttonListSearch.Excel.loading(false); //غیرفعال کردن حالت لود شدن دکمه اکسل
                    });

the function called调用的函数

        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;
        }

Given the constraint of not changing exportfile function as per comment鉴于不根据评论更改exportfile功能的约束

the case is I don't have this ability to change the exportfile function because it has side affects on other functions情况是我没有更改exportfile功能的能力,因为它对其他功能有副作用

the best way to handle this is as follows处理这个问题的最佳方法如下

let req = dataService.exportfile('get', '/api/overtimeedari/exporttoexcle/', model);
req.addEventListener('loadend', () => {
  // do what's needed here
});

since exportfile returns the XMLHttpRequest object, you can listen for the loadend event and do whatever it you're doing there由于exportfile返回 XMLHttpRequest 对象,因此您可以侦听loadend事件并在那里执行任何操作

Note, the loadend event is triggered regardless of success or failure注意,无论成功还是失败都会触发loadend事件

You could do the above with the load event if you want too - but, I'm unsure what order如果您愿意,您也可以使用load事件执行上述操作 - 但是,我不确定什么顺序

x.onload=() => {};
x.addEventListener('load', () => {});

are fired ... also note, do NOT被解雇了……还要注意,不要

req.onload=() => {};

since that would overwrite the onload callback inside the function因为这会覆盖函数内的 onload 回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM