2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptでfetchを使う方法

Last updated at Posted at 2018-12-28

動くコード
出典: 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()に変えると動きました。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?