-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.js
109 lines (71 loc) · 1.99 KB
/
news.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function search()
{
var xhr1 = new XMLHttpRequest();
var url = "http://localhost:3000/cities";
// Called whenever the readyState attribute changes
xhr1.onreadystatechange = function() {
// Check if fetch request is done
if (xhr1.readyState == 4 && xhr1.status == 200) {
// Parse the JSON string
var jsonData = JSON.parse(xhr1.responseText);
console.log(jsonData[0]);
showcities(jsonData);
}
};
xhr1.open("GET", url, true);
xhr1.send();
}
function showcities(data)
{
var output;
var i;
for(var i in data.city)
{
output +="<options>"+ data.city[i]+"</options>";
}
document.getElementById("city_options").innerHTML=output;
}
var city;
function getCity()
{
if(!city)
city="mumbai";
}
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://newsapi.org/v2/everything?q="+city+"&from=2019-10-02&to=2019-10-02&sortBy=popularity&apiKey=3bc87b30a60a40e6a3de082e69829788");
xhr.send();
xhr.onload=function()
{
var data = JSON.parse(this.responseText)
if (xhr.status >= 200 && xhr.status < 400)
{
arr_data=data.articles;
document.getElementById("newsid").innerHTML= `
<h3 class="news">All the news in ${city}</h3>
${arr_data.map(function(arr){
return `
<div class="card">
<div class="card-body">
<div class="card-title">Title:${arr.title}</div>
<u><p>-by${arr.author}source(${arr.source.name})</p></u>
<div class="card-text" style="background-color:black; color:white;">${arr.description}</div>
<a href="${arr.url}" class="btn btn-primary">Original source</a>
</div>
</div>
<br><hr>`
}).join("")}
<p class="footer"></p>
`
}
else {
console.log('error pata nahi')
}
}
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(JSON.parse(this.responseText));
}else
{
console.log('error');
}
});