2016-09-23 62 views
0

是否有任何方式通過angularjs的$ http訪問xhr.responseURL?

有一篇關於MDN的文章解釋瞭如何處理raw xhr。但我找不到任何解決方案來使用angularjs訪問xhr對象。

+0

看到這個http://stackoverflow.com/questions/16532639/access-raw-xhr -object-使用-HTTP – GANI

回答

1

我找到了一個解決方案通過修改$ xhrFactory

angular.module('myApp', []) 
.factory('$xhrFactory', ['$rootScope', ($rootScope) => { 
    return function createXhr(method, url) { 

     // configure the xhr object that will be used for $http requests 
     const xhr = new window.XMLHttpRequest({ mozSystem: true }); 

     // attach an handler 
     xhr.onreadystatechange =() => { 
      if (xhr.readyState === XMLHttpRequest.DONE) { 

       /* 
       you can restrict local xhr calls 
       if (xhr.responseURL.startsWith('file://')) { 
        return; 
       } 
       */ 

       // broadcast xhr object to root scope 
       $rootScope.$broadcast('xhrDone', xhr); 
      } 
     }; 
     return xhr; 
    }; 
}]) 
.controller('MyCtrl', ['$scope', ($scope) => { 

    $scope.$on('xhrDone', (event, xhr) => { 
     // here is the responseURL 
     console.log(xhr.responseURL); 
    }); 

}]) 
相關問題