31 lines
496 B
JavaScript
31 lines
496 B
JavaScript
|
let http={
|
||
|
getVilles(nom){
|
||
|
// À compléter
|
||
|
let final_json;
|
||
|
return fetch('https://geo.api.gouv.fr/communes?nom=' + nom + '&fields=departement&boost=population&limit=7')
|
||
|
.then(readResponseAsJson)
|
||
|
.then(function(body) {
|
||
|
final_json = new Array();
|
||
|
body.forEach(el => final_json.push(el.nom));
|
||
|
return final_json
|
||
|
})
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function readResponseAsJson(response){
|
||
|
|
||
|
if (!response.ok) {
|
||
|
|
||
|
console.log(response.statusText);
|
||
|
}
|
||
|
|
||
|
else {
|
||
|
return response.json()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|