-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
61 lines (55 loc) · 1.76 KB
/
index.html
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
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<body>
<script type="text/javascript">
$(document).ready(function() {
console.log("start fetching data");
var latLngFromRequest = getQueryParams();
console.log(latLngFromRequest);
if(location.search.includes("lat") && location.search.includes("lng")) {
getData(latLngFromRequest)
} else {
console.log("invalid url");
document.getElementById("body").innerHTML = JSON.stringify({'message':'invalid url',
'sample':'https://hereyubaraj.github.io?lat=34.3434&lng=33.3434'});
}
});
function getQueryParams() {
var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]});
return queryDict;
}
function getData(latLng) {
const lat = latLng['lat'];
const lng = latLng['lng'];
console.log("lat = "+lat +"and lng = " + lng);
var mUrl = "https://sun.p.rapidapi.com/api/sun/?latitude="+lat+"&longitude="+lng;
console.log(mUrl);
var settings = {
"async": true,
"crossDomain": true,
"url": mUrl,
"method": "GET",
"headers": {
"x-rapidapi-host": "sun.p.rapidapi.com",
"x-rapidapi-key": "0e798ddf9cmsh08d5343e2e9b304p14e4c3jsnbabbc54429e3"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
// parse the json
var obj = JSON.parse(response);
var newObj = {};
obj.forEach(function(item) {
var key = Object.keys(item);
newObj[key]= new Date(item[key]);
});
document.getElementById("body").innerHTML = JSON.stringify(newObj);
});
}
</script>
<div id="body">
</div>
</body>
</html>