如何使用JavaScript中的Fetch下载Json? [英] How do download of Json using Fetch in JavaScript?
本文介绍了如何使用JavaScript中的Fetch下载Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从Fetch URL下载所需的JSON?
how do download of a JSON required from Fetch URL?
下载文件位于XLSX中.
Download is in XLSX.
CODE
function teste (){
alert(fetch ("url")
.then(response => response.json())
.then(data => { console.log(data)})
.then(response => response.blob())
.then(blob => {
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = "filename.xlsx";
a.click();
})
)
}
推荐答案
从.then()
中删除alert()
,return
值.注意Response
只能读取一次
Remove alert()
, return
value from .then()
. Note Response
can only be read once
function teste() {
fetch("url")
.then(async(response) => {
let clone = response.clone();
let res = await clone.json();
console.log(res);
return response.blob()
})
.then(blob => {
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = "filename.xlsx";
a.click();
})
.catch(function(err) {
console.error(err)
})
}
这篇关于如何使用JavaScript中的Fetch下载Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文