-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetFare.js
77 lines (58 loc) · 3.04 KB
/
getFare.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
let trainNumber0 = document.getElementById("trainNumber0");
let fromCode0 = document.getElementById("fromCode");
let toCode0 = document.getElementById("toCode");
let btn0 = document.querySelector('.submit-btn0');
let flag0 = true;
let resultBox0 = document.querySelector('.resultBox0');
let tableAppend0 = document.getElementById('tableAppend0');
btn0.addEventListener('click',()=>{
event.preventDefault(); // Stops webpage from reloading automatically after form submission
console.log(trainNumber0.value);
console.log(fromCode0.value);
console.log(toCode0.value);
let classType1 = document.getElementById('classType1');
let classType1general = document.getElementById('classType1general');
let classType1tatkal = document.getElementById('classType1tatkal');
let classType2 = document.getElementById('classType2');
let classType2general = document.getElementById('classType2general');
let classType2tatkal = document.getElementById('classType2tatkal');
let classType3 = document.getElementById('classType3');
let classType3general = document.getElementById('classType3general');
let classType3tatkal = document.getElementById('classType3tatkal');
let classType4 = document.getElementById('classType4');
let classType4general = document.getElementById('classType4general');
let classType4tatkal = document.getElementById('classType4tatkal');
if(trainNumber0.value=="number" || fromCode.value=="code" || toCode.value=="code"){
alert("Please fill all the details");
}
else{
// 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/v2/getFare?trainNo=${trainNumber0.value}&fromStationCode=${fromCode.value}&&toStationCode=${toCode.value}`, options)
.then(response => response.json())
.then(response => {
console.log(response);
if(response.data.general.length == 0 && response.data.tatkal.length == 0){
alert('No such train route exists');
return;
}
// Show resultBox
if(flag0){
resultBox0.style.display = 'grid';
flag0 = false;
}
for(let i=0; i<response.data.general.length; i++){
let tr = document.createElement('tr');
tr.innerHTML = `<td><a href="#">${response.data.general[i].classType}</a></td><td class="amount">${response.data.general[i].fare}</td><td class="amount">${response.data.tatkal[i].fare}</td>`;
tableAppend0.appendChild(tr);
}
})
.catch(err => console.error(err));
}
});