Javascript使用一个AJAX函数的结果来生成另一个



我有一个javascript函数,它使用AJAX从国家名PHP文件中获取坐标。

$("#innerSelect").on("change", () => {     //Handles changing the country select.
addCountryBorder($("#innerSelect").val());  /* Calls function that adds country border to map */

$.ajax({                             
url: "libs/php/getCoordsFromCountryName.php",
type: "GET",
dataType: "json",
data: {
countryName: $("#innerSelect").val(),

},
success: function(result) {

if (result.status.name == "ok") {

map.panTo(new L.LatLng(result["data"][0]["geometry"]["lat"] , result["data"][0]["geometry"]["lng"]))

}

},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}

})
});

这些坐标(lat和long(可以用于从不同的API获取国家代码,我如何制作另一个AJAX函数,如下面所示,从上面获取lat/long并使用它导航到正确的国家代码?

function getCountryCode(countryName) {

$.ajax({
url: "assets/php/countryCode.php",
type: "GET",
dataType: "json",
data: {
lat and long
},
success: function(result) {

GET THE COUNTRY CODE
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
})
}

我尝试过,但没有成功


let latitude; 
let longitude;
$("#innerSelect").on("change", () => {     //Handles changing the country select.
addCountryBorder($("#innerSelect").val());  /* Calls function that adds country border to map */

$.ajax({                             
url: "libs/php/getCoordsFromCountryName.php",
type: "GET",
dataType: "json",
data: {
countryName: $("#innerSelect").val(),

},
success: function(result) {

if (result.status.name == "ok") {

latitude = (result["data"][0]["geometry"]["lat"])
longitude = (result["data"][0]["geometry"]["lng"])
map.panTo(new L.LatLng(result["data"][0]["geometry"]["lat"] , result["data"][0]["geometry"]["lng"]))

}

},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}

})
});

let countryCode; 
function getCountryCode(countryName) {

$.ajax({
url: "assets/php/countryCode.php",
type: "GET",
dataType: "json",
data: {
lat: latitude,
lng: longitude
},
success: function(result) {
console.log(JSON.stringify(result));
if (result.status.name == "ok") {
$('#countryCode').html(result['data'][0]['countryCode']);
countryCode = (result['data'][0]['countryCode']);

}

},
error: function(jqXHR, textStatus, errorThrown) {
// your error code
}
}); 

}

function countryInfo() {
$.ajax({
url: "assets/php/countryInfo.php",
type: 'POST',
dataType: 'json',
data: {
country: countryCode,
},
success: function(result) {
if (result.status.name == "ok") {
coords.push({currencyCode: result.data[0].currencyCode});

let formattedPopulation = result['data'][0]['population'].toString().replace(/B(?=(d{3})+(?!d))/g, ",");
let formattedArea = Math.round(result['data'][0]['areaInSqKm']).toString().replace(/B(?=(d{3})+(?!d))/g, ",");
$('#continent').html(result['data'][0]['continentName']);
$('#capital').html(result['data'][0]['capital']);
$('#countryName').html(result['data'][0]['countryName']);
$('#population').html(formattedPopulation);
$('#currencyCode').html(result['data'][0]['currencyCode']);
$('#area').html(formattedArea);
currencyConverter();
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR)
console.log(textStatus)
console.log(errorThrown)
}
});
}

尝试将您的第一个ajax请求放在类似的函数上

function fetchApi() {
return jQuery.ajax({
url: 'yoururl',
method: 'GET',
dataType: 'json',
success: function (result) {
//return the value from the request
return result;
}
});
}

现在,您可以在第一个请求之后调用第二个请求,并将lat和long值传递给

fetchApi().then((data) => //do something with the data)

相关内容

  • 没有找到相关文章

最新更新