Is it a way to extract a var from xmlhttp2 request in js?

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);}
 };"GET", "js/test3.php",true);

im trying to extract the "arrond" var as a json

Thanks for your help !


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 ='map1');
var osmUrl='http://{s}{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}{z}/{x}/{y}.png';
var osmhuman = new L.TileLayer(osmhumanUrl, {attribution: osmAttrib}).addTo(map1); 

var Esri_WorldImagery = L.tileLayer('{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 =
        let response = JSON.parse(xhttp2.responseText)
//         //appel de la couche
//      //console.log(response[0])
       let arrond = L.geoJSON(response).addTo(map1); 
     };"GET", "js/test3.php",true);

let Jardins =  L.geoJSON(jardins, 
        onEachFeature: function (feature, layer) {

//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

Read more here:

Content Attribution

This content was originally published by Veko047 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: