简体   繁体   English

待处理的HTTP请求期间出现Angular js页面重新加载错误

[英]Angular js page reload error during pending http requests

In a single page angular App during the time of ongoing http get requests (before the page is fully loaded) , if i reload the page , all the http request errorcallbacks are called . 在正在进行的http get请求期间(在页面完全加载之前)的单个页面angular App中,如果我重新加载页面,则将调用所有http请求错误回调。 I use google chrome. 我使用谷歌浏览器。 Save the below code in an html file and reload the page just after it is open to reproduce the problem. 将下面的代码保存在html文件中,并在打开页面后重现该页面,以重现该问题。 This breaks the whole page because it shows alerts from all the APIs being called. 这会中断整个页面,因为它显示了来自所有正在调用的API的警报。 If there are 50 pending http requests i get 50 error alert on reloading the page during the http request.I donot want to remove the alert from the errorCallBack function of the http get. 如果有50个待处理的http请求,则在http请求期间重新加载页面时会收到50条错误警报。我不想从http get的errorCallBack函数中删除警报。 If i reload the page after all the ajax http requests are completed then it doesnot show any error. 如果在所有ajax http请求完成后重新加载页面,则它不会显示任何错误。 Please help me get around this problem. 请帮助我解决这个问题。

<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: I am really struck on this. ps:我真的很震惊。 Please help me get around it. 请帮助我解决它。 Above is a minimalistic question i have made to reproduce the problem. 以上是我为重现该问题而提出的一个简约问题。 Actual web Application i have been working on : https://codemachin.github.io/gameofthrones/ 我一直在研究实际的Web应用程序: https : //codemachin.github.io/gameofthrones/

BookService.getAllHouses([i])
.then(function successCallback(response) {

  if(response.data.length>0){
      main.all.push.apply(main.all,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);
  //IMPORTANT
  throw response;
});

When a .catch handler fails to re-throw an error, it converts a rejected promise to a successful promise. .catch的处理程序无法重新抛出异常,将它转换被拒绝的承诺,一个成功的承诺。


One approach is to reduce the number of alerts is to use $q.all to provide a single rejection handler. 一种减少警报数量的方法是使用$ q.all提供单个拒绝处理程序。

this.allHouses = function(){
    var promiseList = [];
    for(let i=1;i<12;i++){
      var promise = BookService.getAllHouses([i])
        .then(function onSuccess(response) {        
          if(response.data.length>0){
              main.all.push.apply(main.all,response.data);
          }
          return response.data;
      });
      promiseList.push(promise)
    };

    return $q.all(promiseList)       
      .catch( function onRejection(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);
          throw response;
    });
}

With $q.all , if any of the promises is settled with a rejection, the resulting promise will be rejected with the rejection value of the first rejected promise. 使用$ q.all ,如果任何承诺以拒绝方式结算,则所产生的承诺将以第一个被拒绝的承诺的拒绝值被拒绝。

This example improves the allHouses function by having it return a promise that can be used for further chaining. 此示例通过使它返回可用于进一步链接的promise,改进了allHouses函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM