2017-09-28 245 views
1

我需要一個eventlistener來通知用戶他們對API的resquest狀態。http請求響應的eventlistener

 switch (category) { 
      case 'B': 
       console.log('creating basis'); 
       break; 
      case 'B-T': 
       console.log('creating basis top'); 
       kladdService.create_B_T(order); 
       break; 
      case 'B-M-T': 
       console.log('creating basis, mellom, top'); 
       kladdService.create_B_M_T(order); 
       break; 
      case 'B-M-M-T': 
       console.log('creating basis'); 
       kladdService.create_B_M_M_T(order); 
       break; 

      case 'Mix_Minimum2B-M-T': 
       kladdService.create_Mix_Minimum2B_M_T(order); 
       break; 
      case 'Mix_Minimum2M-T': 
       kladdService.create_Mix_Minimum2M_T(order); 
       break; 
     } 
     console.log('order ble sent: '); 
     console.log(order); 
     //trenger en if her som venter på en http 200 response 
     var url = 'http://localhost:32605/api/Kladd/' + category; 
     console.log(url); 
     var xhr = new XMLHttpRequest(); 
     console.log(xhr.status); // 0 so either unsendt or opened 
     logService.info('prduktet har fått en ID'); 
    }; 

開關盒工作正常,但處理的創建方法的狀態只在devtools的網絡面板中可見。

我假設我需要xhr變量,並基於xhr.status通知用戶結果。

這裏是從網絡面板devtools

info from network panel

+0

您是否嘗試過做類似的東西' xhr.onreadystatechange = function(){}'並檢查readyState和status? – Matt

回答

0

引用id varaible從kladdService服務,我得到了想要的結果返回的輸出:

 switch (category) { 
      case 'B': 
       console.log('creating basis'); 
       break; 
      case 'B-T': 
       console.log('creating basis top'); 
       $scope.produktKladdId = kladdService.create_B_T(order); 
       break; 
      case 'B-M-T': 
       console.log('creating basis, mellom, top'); 
       $scope.produktKladdId = kladdService.create_B_M_T(order); 
       break; 
      case 'B-M-M-T': 
       console.log('creating basis'); 
       $scope.produktKladdId = kladdService.create_B_M_M_T(order); 
       break; 

      case 'Mix_Minimum2B-M-T': 
       $scope.produktKladdId = kladdService.create_Mix_Minimum2B_M_T(order); 
       break; 
      case 'Mix_Minimum2M-T': 
       $scope.produktKladdId = kladdService.create_Mix_Minimum2M_T(order); 
       break; 
     } 
     console.log(order); 

     if ($scope.produktKladdId) { // if this is not defined, an error appears 
      console.log($scope.produktKladdId); 
      logService.info('prduktet har fått en ID'); 
     } else { 
      logService.error('noe gikk galt'); 
     }