Process chunked response with angularjs $http

不羁岁月 提交于 2019-12-01 02:58:01

问题


I discoverd recently chunked response. I agree that most of the time we want to work on a full response. But what if I want to work on a chunked response.

How would i do this with the $http service??


回答1:


You can define a function with the angularjs promise $q wrapping the XMLHttpRequest.

var chunkedRequestWithPromise = function () {
  var deferred = $q.defer();
  var xhr = new XMLHttpRequest()
  xhr.open("GET", 'https://yoururl.com/chunked', true)
  xhr.onprogress = function () {
    deferred.notify(xhr.responseText);
  }
  xhr.onreadystatechange = function (oEvent) {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            deferred.resolve('success');
        } else {
            deferred.reject(xhr.statusText);
        }
    }
  };
  xhr.send();
  return deferred.promise;
};

and use it:

chunkedRequestWithPromise().then(successFn,errorFn,notifyFn);



回答2:


I've found a way to do something that looks correct to me http://www.igvita.com/2011/08/26/server-sent-event-notifications-with-html5/

Of course there is also the websocket api, but it seams heavy for my uses

Another possibility would be the write another http service which would give control over the http response



来源:https://stackoverflow.com/questions/13169617/process-chunked-response-with-angularjs-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!