我在Google上搜索了两个JS问题,却找不到答案:
a)我尝试加载没有扩展名的文件,但出现错误。
b)我找不到加载文件并等待文件完成的解决方案。
下面的URL是真实文件,因此您可以自己尝试。
// ad a)
var wl_version = "";
var wl_wkt = "";
function download(nr, url) {
var client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = function() {
if (nr == 1) {wl_version = client.responseText;}
if (nr == 6) {wl_wkt = client.responseText;}
}
client.send();
}
function getdata() {
download(1, "https://data.wien.gv.at/csv/wienerlinien-ogd-version.csv"); // THAT WORKS !!!
download(6, "https://go.gv.at/l9ogdwienerlinienteilstreckenwkt"); // THAT DOESN'T WORK !!!
}
// ----------------------------------------------
// ad b)
function httpRequestAsync(url, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4 && httpRequest.status == 200) { // THAT DOESN'T WORK, FUNCTION DOESN'T WAIT UNTIL DOWNLOAD HAS FINISHED
callback(httpRequest.responseText);
}
}
httpRequest.open("GET", url, true);
httpRequest.send();
}
答案 0 :(得分:0)
根据我的经验,在通过javascript下载之前和之后都无法附加事件。出于安全原因,浏览器不允许此类事件。
但是要下载文件,您还可以使用javascript在新标签中打开文件的网址。
window.open('https://data.wien.gv.at/csv/wienerlinien-ogd-version.csv', '_blank')