Im new to javascript and php prog.
Im trying to extract a var from xhttp2 request to reuse it in my code. After multiply test I cant find a way to do it. I think i dont really get how asynchronous function works ..
my code :
xhttp2.onreadystatechange = function() {
if (this.readyState == 4 && this.status ==200) {
let response = JSON.parse(xhttp2.responseText)
let arrond = L.geoJSON(response).addTo(map1);}
};
xhttp2.open("GET", "js/test3.php",true);
xhttp2.send();
im trying to extract the "arrond" var as a json
Thanks for your help !
Victor
here is my full code : it is on a leaflet project. The xhttp request is use to add data to my lealet map. The data comes from a postgreSQL data base and a php code to an array.
//Initialisation de la carte
var map1 = L.map('map1');
var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © OpenStreetMap contributors';
var osm = new L.TileLayer(osmUrl, {attribution: osmAttrib});
map1.setView([45.76, 4.85], 12);
//ajout de basemaps supplémentaires
var osmhumanUrl='https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png';
var osmhuman = new L.TileLayer(osmhumanUrl, {attribution: osmAttrib}).addTo(map1);
var Esri_WorldImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
});
// //Appel de la couche equipement
var xhttp2 = new XMLHttpRequest();
// //lecture de la connexion au fichier php (2 variables cf. biblio)
xhttp2.onreadystatechange = function() {
if (this.readyState == 4 && this.status ==200) {
// //récupération du résultat de la requête sql et parcours de la couche :
document.getElementById("demo").innerHTML =
(xhttp2.responseText);
let response = JSON.parse(xhttp2.responseText)
// //appel de la couche
// //console.log(response[0])
let arrond = L.geoJSON(response).addTo(map1);
}
};
xhttp2.open("GET", "js/test3.php",true);
xhttp2.send();
let Jardins = L.geoJSON(jardins,
{
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.title);
}
}).addTo(map1);
//groupe de couche et de basemaps et layerControl
var baseMaps = {
"OSM": osm,
"OSM Humanitarian": osmhuman,
"Satellite": Esri_WorldImagery
};
var overlayMaps = {
"Initiatives existantes": Jardins
};
//Ajout d'un bouton de gestion des calques
L.control.layers(baseMaps).addTo(map1);