-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearchStation.js
54 lines (39 loc) · 1.7 KB
/
searchStation.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
let stationCode8 = document.querySelector("#stationCode8");
let tableAppend8 = document.querySelector('#tableAppend8');
let submitbtn8 = document.querySelector('.submit-btn8');
let flag8 = true;
let resultBox8 = document.querySelector('.resultBox8');
submitbtn8.addEventListener('click',()=>{
event.preventDefault(); // Stops webpage from reloading automatically after form submission
if(stationCode8.value=="code"){
alert("Please select station code");
return;
}
// API Call
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': 'a1e0032c9amsh0c59e81fe5967c8p1e443bjsn602da0c29fd4', // Change rapidapi key here
'X-RapidAPI-Host': 'irctc1.p.rapidapi.com'
}
};
fetch(`https://irctc1.p.rapidapi.com/api/v1/searchStation?query=${stationCode8.value}`, options)
.then(response =>response.json())
.then(response =>{
console.log(response);
for(let i=0; i<response.data.length; i++){
if(response.data[i].code=="BJU"){
let tr8 = document.createElement('tr');
tr8.innerHTML = `<td><a href="#">${response.data[i].eng_name}</a></td><td class="amount">${response.data[i].state_name}</td>`;
tableAppend8.appendChild(tr8);
break;
}
}
})
.catch(err => console.error(err));
// Show resultBox
if(flag8){
resultBox8.style.display = 'grid';
flag8 = false;
}
});