gpt4 book ai didi

javascript - Jquery AjaxStop 未触发,而 AjaxStart 则触发

转载 作者:行者123 更新时间:2023-12-03 02:30:57 25 4
gpt4 key购买 nike

我在体验 API 时发现自己在某些方面遇到了阻碍。

我有一个从 api 获取数据的 Jquery 函数,为了不产生“重新定位”效果,我需要该函数等待所有调用,然后再触发另一个将放置所有 api 的函数日期到位。

我尝试使用 AjaxStart 和 AjaxStop,但是虽然第一个完美地工作,但第二个却不能,我不明白为什么......

我正在构建的应用程序非常大,所以我制作了一个 codePen 来简化我的问题:

HTML:

<div id="button"></div>
<div id='locationbox'>
<span id='city'>Get the country</span>
<img id = "countryimg" src="">
<div id='searching'> searching...</div>
</div>

CSS

#button{
height : 150px;
width : 150px;
border : 5px solid black;
border-radius : 50%;
position : absolute;
top : 45%;
left : 45%;
}
#locationbox{
text-align : center;
width : 300px;
position : absolute;
top : 25%;
left : 38.5%;

}

JavaScript:

$("#button").on('click',function(){
getCity();

});

$(document).ajaxStart(function() {
$("#city").html("fetching data");

});
$(document).ajaxStop(function() {
$("#searching").html("Success")
});

function getCity(){
$.ajax({
datatype : "json",
contentType : "application/json",
type : "GET",
url : "https://nominatim.openstreetmap.org/reverse?",
data : {
format:"json",
lat :"50.8503",
lon : "4.3517",
zoom:"18",
adressdetails:"1"
},
success : function(quote){
var city = quote.address.city;
var country = quote.address.country
var countryCode = quote.address.country_code.toUpperCase()
document.getElementById("city").innerHTML = city + " , " + country;
document.getElementById("countryimg").src = "http://www.geognos.com/api/en/countries/flag/" + countryCode + ".png";
document.getElementById("country").innerHTML = country;
},
error : document.getElementById("city").innerHTML = "error!"
});
};

正如您在 this code pen 中看到的那样当 AjaxStart 触发时,AjaxStop 不会触发。感谢您的阅读并感谢大家对我的帮助。

最佳答案

发生这种情况是因为您的成功代码块中存在错误。由于此错误,您的 ajaxStop 不会执行。

删除行 document.getElementById("country").innerHTML = Country; 因为文档中没有具有该 ID 的元素。否则,添加一个 id 为 country

的元素

检查工作代码笔 https://codepen.io/anon/pen/NyjMOQ

关于javascript - Jquery AjaxStop 未触发,而 AjaxStart 则触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com