通过嵌套的 ajax 传递从 JSON 检索的对象并将数据传递到同一类中的另一个方法

问题描述 投票:0回答:1

_border(e) { 让 countryCode = e;

$.ajax({
  url: "php/borderGeo.php",
  type: "POST",
  dataType: "json",
  success: function (result) {
    // console.log(JSON.stringify(result));

    const filterData = result.data.features.filter(
      (a) => a.properties.iso_a2 === countryCode
    );
    {
      let coords = filterData[0];  THE DATA I NEED TO PASS INTO  L.geoJSON
   
    }
  },

});

} //////////////////////////////////////////////// ///////////////////////////////

类应用{

_foundPosition(位置){

L.geoJSON(DATA PASSED IN FROM AJAX).addTo(this.#map);  

// Passes coordinates to methods

this.#map.on("click", this._weatherCall.bind(this));
this.#map.on("click", this._phpApiCall.bind(this));
//layer controller
L.Control.geocoder().addTo(this.#map);
L.control.layers(mapCollection).addTo(this.#map);
L.easyButton('<img src="img/icons8-country-50.png">', function () {
  var myModal = new bootstrap.Modal(document.getElementById("modalOne"), {
    keyboard: false,
  });
  var modalToggle = document.getElementById("modal"); // relatedTarget
  myModal.show(modalToggle);
}).addTo(this.#map);

/////////////////////////////////////////////// //////////////

_border(e) { 让 countryCode = e;

$.ajax({
  url: "php/borderGeo.php",
  type: "POST",
  dataType: "json",
  success: function (result) {
    // console.log(JSON.stringify(result));

    const filterData = result.data.features.filter(
      (a) => a.properties.iso_a2 === countryCode
    );
    {
      let coords = filterData[0]; THE DATA I NEED TO PASS INTO  L.geoJSON
    }
  },
});

}

}

const app = new App();

javascript json ajax class object
1个回答
0
投票

为了将过滤后的 JSON 数据从 AJAX 调用传递到 App 类的 _foundPosition 方法中,您可以进行以下修改:

  1. 更改 _foundPosition 方法签名以接受参数,比如坐标。
  2. 获取过滤后的JSON数据后,以坐标为参数调用_foundPosition方法
$.ajax({
  url: "php/borderGeo.php",
  type: "POST",
  dataType: "json",
  success: function (result) {
    const filterData = result.data.features.filter(
      (a) => a.properties.iso_a2 === countryCode
    );
    let coords = filterData[0]; // THE DATA I NEED TO PASS INTO L.geoJSON
    app._foundPosition(coords); // Call the _foundPosition method with coords as an argument
  },
});
} //////////////////////////////////////////////////////////////////////////////////

class App {

_foundPosition(coords) { // Add a parameter to accept the coords

L.geoJSON(coords).addTo(this.#map); // Use the passed coords

// Passes coordinates to methods

this.#map.on("click", this._weatherCall.bind(this));
this.#map.on("click", this._phpApiCall.bind(this));
//layer controller
L.Control.geocoder().addTo(this.#map);
L.control.layers(mapCollection).addTo(this.#map);
L.easyButton('<img src="img/icons8-country-50.png">', function () {
  var myModal = new bootstrap.Modal(document.getElementById("modalOne"), {
    keyboard: false,
  });
  var modalToggle = document.getElementById("modal"); // relatedTarget
  myModal.show(modalToggle);
}).addTo(this.#map);
///////////////////////////////////////////////////////////////

_border(e) { let countryCode = e;

$.ajax({
  url: "php/borderGeo.php",
  type: "POST",
  dataType: "json",
  success: function (result) {
    const filterData = result.data.features.filter(
      (a) => a.properties.iso_a2 === countryCode
    );
    let coords = filterData[0]; // THE DATA I NEED TO PASS INTO L.geoJSON
    app._foundPosition(coords); // Call the _foundPosition method with coords as an argument
  },
});
}

}

const app = new App();

如果你遇到任何问题,请告诉我?试图模仿你的电话,这可能会导致问题。

© www.soinside.com 2019 - 2024. All rights reserved.