JavaScriptでfetchを使う方法
2852 ワード
動くコード
出典: https://sbfl.net/blog/2017/01/29/fetch-api/
fetch(url)
.then((response) => response.text())
.then((text) => console.log(text))
.catch((error) => console.log(error));
動かないコード
出典: https://developer.mozilla.org/ja/docs/Web/API/Fetch_API/Using_Fetch
fetch('http://example.com/movies.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
なぜ本家のmozillaのコードが動かないのか謎。。。
追記:response.json()がExceptionを出していた模様。text()に変えると動きました。
Author And Source
この問題について(JavaScriptでfetchを使う方法), 我々は、より多くの情報をここで見つけました https://qiita.com/Nelson605/items/45fd811976538a99ae71著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .