2017-10-17 15 views
0

フェッチURLからJSONをダウンロードするにはどうすればよいですか?Fetch in JavaScriptを使用してJsonをダウンロードするにはどうすればよいですか?

ダウンロードは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();      
 
     }) 
 
) 
 
}

+1

'alert()'の目的は何ですか?第2の '.then()'から返される値はありません – guest271314

+0

第1の '.then()'は第2の '.then()' 'の応答を意味する' data'を宣言していません。は未定義です。 – zero298

+0

テストのために、私はフォームが正しいかどうかわかりません。 – Jota

答えて

0

.then()からalert()return値を削除します。注:Responseは一度だけ読み取ることができます

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) 
    }) 
} 
関連する問題