Export excel using XMLHttpRequest

problem description

use XMLHttpRequest to export excel, but responseType may be blob, or json

related codes

    var xhr = new XMLHttpRequest();
    xhr.open("get", url, true);
    xhr.responseType = "blob"; // blob  blob 
    xhr.onload = function () {
        console.log(xhr)
        if (this.status === 200) {
            var blob = this.response;
            var reader = new FileReader();
            reader.readAsDataURL(blob); // base64ahref
            reader.onload = function (e) {
                var a = document.createElement("a"); // a
                a.download = name + ".xls";
                a.href = e.target.result;
                $("body").append(a); // firefoxclick
                a.click();
                $(a).remove();
            };
        }
    }
    xhr.send(); // ajax

what result do you expect? What is the error message actually seen?

response may not be a file, but a wrong prompt (there is a mistake in the export process or there is no data export). At this time, what if responseType has been set to blob? Can responseType be defined only before the request is made?

Mar.30,2021

xhr.getResponseHeader("Content-Type")// json blodstringjson
Menu