pis #1
@ -132,16 +132,19 @@ async function getApi(path,auth = false) {
|
|||||||
log(`getApi: Fetching from endpoint: ${url}, Auth=${auth}`)
|
log(`getApi: Fetching from endpoint: ${url}, Auth=${auth}`)
|
||||||
try {
|
try {
|
||||||
var resp = await fetch(url)
|
var resp = await fetch(url)
|
||||||
if (resp != 200) {
|
var json = await resp.json()
|
||||||
|
log(`resp.status: ${resp.status}`)
|
||||||
|
log(`resp.json: ${json}`)
|
||||||
|
if (resp.status != 200) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if (!resp.ok) {
|
if (!resp.ok) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if (resp.json() === "[]") {
|
if (json === "[]") {
|
||||||
return 'empty'
|
return 'empty'
|
||||||
}
|
}
|
||||||
return resp.json();
|
return json;
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
11
js/pis.js
11
js/pis.js
@ -32,18 +32,16 @@ async function insertData(json) {
|
|||||||
<th class="code">Code</th>
|
<th class="code">Code</th>
|
||||||
<th class="stops">Stations</th>
|
<th class="stops">Stations</th>
|
||||||
</tr>`
|
</tr>`
|
||||||
div.insertAdjacentHTML("beforeend", tableHtml)
|
|
||||||
const data = JSON.parse(json)
|
|
||||||
let results = 0
|
let results = 0
|
||||||
for(var i = 0; i < data.length; i++) { // Break into separate function. Need to
|
for(var i = 0; i < json.length; i++) { // Break into separate function. Need to
|
||||||
// iterate over stops and print them in the
|
// iterate over stops and print them in the
|
||||||
// format: `AVN, PRI, SHH... etc.`
|
// format: `AVN, PRI, SHH... etc.`
|
||||||
tableData += `<tr><td class="code">${data[i][code]}</td>
|
tableData += `<tr><td class="code">${json[i]['code']}</td>
|
||||||
<td class="stops">${data[i][stops]}</td></tr>`
|
<td class="stops">${json[i]['stops']}</td></tr>`
|
||||||
results++
|
results++
|
||||||
}
|
}
|
||||||
tableData += "</table>"
|
tableData += "</table>"
|
||||||
document.getElementById('result-table').insertAdjacentHTML("beforeend", tableData)
|
div.insertAdjacentHTML("beforeend", tableData)
|
||||||
document.getElementById('result-count').textContent = results
|
document.getElementById('result-count').textContent = results
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,5 +54,6 @@ async function reset() {
|
|||||||
document.getElementById('origin').value = ""
|
document.getElementById('origin').value = ""
|
||||||
document.getElementById('destination').value = ""
|
document.getElementById('destination').value = ""
|
||||||
document.getElementById('result-box').style = 'display:none'
|
document.getElementById('result-box').style = 'display:none'
|
||||||
|
document.getElementById('result-box').innerHTML = ''
|
||||||
document.getElementById('crs-box').style = 'display:block'
|
document.getElementById('crs-box').style = 'display:block'
|
||||||
}
|
}
|
Reference in New Issue
Block a user