File tree Expand file tree Collapse file tree 1 file changed +12
-18
lines changed
Expand file tree Collapse file tree 1 file changed +12
-18
lines changed Original file line number Diff line number Diff line change 11function getForecast ( ) {
22 let lat = document . coords . lat . value ;
33 let lon = document . coords . lon . value ;
4- fetch_json ( 'https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat=' + lat + '&lon=' + lon ) . then (
5- function ( value ) {
6- if ( value != null ) {
7- document . getElementById ( 'output' ) . value = JSON . stringify ( value , null , 2 ) ;
8- } else {
9- document . getElementById ( 'output' ) . value = 'Failure' ;
10- }
4+ let url = 'https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat=' + lat + '&lon=' + lon ;
5+ fetch ( url ) . then ( function ( res ) {
6+ if ( res . ok ) {
7+ res . json ( ) . then ( function ( data ) {
8+ document . getElementById ( 'output' ) . value = JSON . stringify ( data , null , 2 ) ;
9+ } ) ;
10+ } else {
11+ document . getElementById ( 'output' ) . value = res . status ;
1112 }
12- ) ;
13- }
14-
15- async function fetch_json ( url ) {
16- let obj = null ;
17- try {
18- obj = await ( await fetch ( url ) ) . json ( ) ;
19- } catch ( e ) {
20- console . log ( e , url ) ;
21- }
22- return ( obj ) ;
13+ } , function ( e ) {
14+ console . log ( "Fetch failed!" , e ) ;
15+ document . getElementById ( 'output' ) . value = e ;
16+ } ) ;
2317}
2418
2519// Usage in HTML
You can’t perform that action at this time.
0 commit comments