在正在进行的http get请求期间(页面完全加载之前)的单个页面angular App中,如果我重新加载页面,则调用所有http请求errorcallbacks。我使用谷歌浏览器。 将下面的代码保存在html文件中,并在打开页面后重新加载页面以重现问题。 这打破了整个页面,因为它显示了所有被调用的API的警报。 如果有50个挂起的http请求,我在http请求期间重新加载页面时得到50个错误警报。我不想从http get的errorCallBack函数中删除警报。如果我在所有ajax http请求完成后重新加载页面,则它不会显示任何错误。 请帮我解决这个问题。在挂起的http请求期间Angular js页面重新加载错误
<div ng-app="iceApp" ng-controller="allCharacterController as myCharacter">
<input type="text" id="search1" ng-model="search.name">
<div ng-repeat="book in myCharacter.character | filter : search">
{{book.name}}
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('iceApp', []);
myApp.controller('allCharacterController',['$http',function($http) {
var main = this;
this.character=[];
this.baseUrl = 'https://www.anapioficeandfire.com/api/';
// there were 50 pages to get data from thats why i have made a loop
this.allCharacters = function(){
for(i=1;i<50;i++){
$http({
method: 'GET',
url: main.baseUrl+"characters?page="+i+"&pageSize=50"
})
.then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response.data);
if(response.data.length>0){
main.character.push.apply(main.character,response.data);
}
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
alert("some error occurred. Check the console.");
console.log(response);
});
}
}// end load all blogs
this.allCharacters();
}])
</script>
ps:我对此很感兴趣。请帮我解决它。以上是我为重现问题所做的一个简约问题。 实际的Web应用程序我一直在努力:https://codemachin.github.io/gameofthrones/
谨慎使用警报和其他模式对话框,因为它们具有破坏性。他们的突然出现迫使用户停止当前的任务并专注于对话内容。有时这是一件好事,但大多数情况下它是不必要的,而且通常很烦人。**考虑替代方法,如内联扩展和敬酒。 – georgeawg
*我仍然在寻找更好的解决方案,因为重新加载是故意的,所以它甚至不会显示单个警报。*最简单的解决方案是删除警报。发生什么错误以及为什么用户需要中断? – georgeawg
之后我纠正了这个错误。我的意思是---我仍然在寻找一个更好的解决方案,因为重新加载是故意的,所以**不会显示单个提醒。 –