-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchapi.js
64 lines (58 loc) · 1.95 KB
/
fetchapi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function showWeather() {
//API Calling Section
//Show Search
document.getElementById("show").style.display="block";
//Select City Name From Search
var city_name = document.getElementById("cityInput").value;
// var author_name = "MH Miyazi";
//Secret Key
var secret_key = '54bc42f490a0a0568f5d20da7c7901ca';
var data_link = 'https://api.openweathermap.org/data/2.5/weather?q='+city_name+'&appid='+secret_key;
//variables
var city;
var description;
var temperature ;
var humidity;
var icon;
//Fethching API Data From URL/data_link
fetch(data_link)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
//Box Data
//********
//storing Data in Variable
//Further We can process anything with variable
//Soo Easily
city = data.name;
description = data.weather[0].description;
get_temperature = data.main['temp'];
temperature = (get_temperature - 273).toFixed(2);
humidity = data.main['humidity'];
icon = 'http://openweathermap.org/img/wn/'+data.weather[0].icon+'.png';
//Set Data to empty data Table
document.getElementById("city").innerHTML = city;
document.getElementById("description").innerHTML = description;
document.getElementById("temperature").innerHTML = temperature;
document.getElementById("humidity").innerHTML = humidity;
document.getElementById("icon").src = icon;
//********
//AJAX Auto POST Method Section
//AJAX method to pass data to insertdata.php
$.ajax({
url:'insert.php',
method:'POST',
data:{
city:city,
description:description,
temperature:temperature,
humidity:humidity
},
success:function(data){
// alert(data);
}
});
});
}