LoginSignup
0
1

More than 3 years have passed since last update.

【脱jQuery】AjaxをFetch APIで実現する

Posted at

ナウいAjaxのやり方として、Fetch APIのテストソースを書きました。
呼び先のAPIは天気を取得するOpenWeatherMapを使用しています。

snippet.js
var API_KEY = 'APIキーを設定';
var lon = '141.355539';
var lat = '43.067885';
var URL = `http://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&cnt=1&units=metric&APPID=${API_KEY}`;


(async ()=>{
    try{
        var res = await fetch(URL);
        var data = await res.json();
        console.log(JSON.stringify(data));
    }
    catch(err){
        console.log(err);
    }
})();
0
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
0
1