Eroare la incarcare json
Scris: Lun Dec 05, 2022
Bună Marius
Cum rezolv să imi încarce tot fișierul .json
eoroare la
fisierul url json este mare, am pus un fisier json mai mic cu acceasi structura si merge
Cum rezolv să imi încarce tot fișierul .json
eoroare la
Cod: Selectaţi tot
//fetch(endpoint)
//.then(blob => blob.json())
//.then(data => cities.push(...data));
fisierul url json este mare, am pus un fisier json mai mic cu acceasi structura si merge
Cod: Selectaţi tot
<!doctype html>
<html lang="en">
<head></head>
<body>
<div class="search-form">
<input type="text" class="search" placeholder="Oraș sau județ">
<ul class="suggestions">
</ul>
</div>
<script>
const endpoint ='raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/cities.json';
const cities = [];
fetch(endpoint)
.then(blob => blob.json())
.then(data => cities.push(...data));
function findMatches(wordToMatch, cities) {
return cities.filter(place => {
const regex = new RegExp(wordToMatch, 'gi');
return place.name.match(regex) || place.state_name.match(regex);
});
}
function displayMatches() {
const matchArray = findMatches(this.value, cities).slice(0, 5);
const html = matchArray.map(place => {
const regex = new RegExp(this.value, 'gi');
const cityName = place.name.replace(regex, `<span class="hl">${this.value}</span>`);
const stateName = place.state_name.replace(regex, `<span class="hl">${this.value}</span>`);
return `<li>
<span class="name">${cityName}, ${place.state_name}</span>
</li>`;
}).join('');
suggestions.innerHTML = html;
}
const searchInput = document.querySelector('.search');
const suggestions = document.querySelector('.suggestions');
searchInput.addEventListener('change', displayMatches);
searchInput.addEventListener('keyup', displayMatches);
</script>
</body>
</html>