alpha_full/_include/_js/fetchCommune.js
2026-04-06 22:58:51 +02:00

91 lines
2.9 KiB
JavaScript
Executable File

// ---------------------------------------------------
// Liste des villes
// ---------------------------------------------------
function fetchCommune (cp, numForm, idF){
debugLocal=0;
if (debugLocal==1) console.log('cp = ' + cp + ' numForm = ' + numForm + ' formId = ' + idF);
var continuer='o';
var tabVilleListe=new Array;
var url='https://apicarto.ign.fr/api/codes-postaux/communes/'+cp;
fetch(url,{
method: "GET",
headers: {
'Content-Type': 'application/json',
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8',
'Access-Control-Allow-Origin':'123ventes.com my-chop.com',
}
// mode: "no-cors"
})
.then(rep =>
{
if (rep.ok === true) return rep.json();
// Ici je traite les erreurs HTTP
else {
alert('Pas de ville avec ce codePostal : '+cp);
return Promise.reject(`Erreur HTTP => ${rep.status}`);
}
}
)
.then(data =>{
if (debugLocal==1) console.log(data);
if (debugLocal==1) console.log(data.length);
if(data.length>0){
for(i=0;i<data.length;i++){
console.log(data[i].nomCommune);
tabVilleListe[i]= data[i].nomCommune;
}
}
else{
alert('Pas de ville avec ce codePostal : '+cp);
continuer='n';
}
// envoyer a php
if(tabVilleListe.length>0){
var faire=0;
if(faire==1){
tabVilleListe = JSON.stringify(tabVilleListe);
$.ajax({
// headers:{"Access-Control-Allow-Origin": "*", "Content-Type": "text/csv"},
type: 'POST',
//cache: false ,
//async: false,
//dataType: 'JSON',
//contentType: 'application/json; charset=utf-8',
url: './index.php?affPageSite=30&affPageClient=2',
data : { result:'test' } ,
success : function(resp) {
alert('Success'+tabVilleListe+' ');
//console.log(data);
// document.adresseFact.submit();
},
error: function(){
alert('something bad happened');
}
});
}//faire
tabVilleListe = JSON.stringify(tabVilleListe);
if(numForm==2){
if (idF=="adresseFact") document.getElementById('codeOpe').value='faireMajFact';
if (document.getElementById('tabVilleListe')) document.getElementById('tabVilleListe').value=tabVilleListe;
if (document.getElementById('codePost')) document.getElementById('codePost').value=cp;
if (document.getElementById('faireCP')) document.getElementById('faireCP').value=1;
}
if(numForm==3){
if (idF=="adresseLiv") document.getElementById('codeOpe').value='faireMajLiv';
if (document.getElementById('tabVilleListeLiv')) document.getElementById('tabVilleListeLiv').value=tabVilleListe;
if (document.getElementById('codePostLiv')) document.getElementById('codePostLiv').value=cp;
if (document.getElementById('faireCPLiv')) document.getElementById('faireCPLiv').value=1;
}
if (continuer=='o') document.getElementById(idF).submit();
}
})
.catch(err => console.log(err) )
console.log('Fin recherche ville');
}