简体   繁体   English

如何将xhr请求转换为有角的$ http请求?

[英]How to convert xhr request to angular $http request?

I am trying to load image via ajax by the following code which is already Working . 我正在尝试通过以下已经正常工作的代码通过ajax加载图像。 I am trying to convert it into angular $http and it doesn't work. 我正在尝试将其转换为有角的$http ,并且不起作用。

Plain ajax code 普通的ajax代码

var xhr = new XMLHttpRequest();
xhr.open('GET', attr.imageUrl, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
    element[0].src = window.URL.createObjectURL(this.response);
};

xhr.send();

Angular Code 角度代码

$http.get(attr.imageUrl, {}, {
    responseType: 'arraybuffer'
})
.success(function(response) {
    var file = new Blob([response]);
    element[0].src = URL.createObjectURL(file);
});

I think it should be: 我认为应该是:

$http.get({ url: attr.imageUrl, responseType: 'arraybuffer'})
.success(function(response) {
    var file = new Blob([response]);
    element[0].src = URL.createObjectURL(file);
});

Or: 要么:

$http.get(attr.imageUrl, {responseType: 'arraybuffer'})
.success(function(response) {
    var file = new Blob([response]);
    element[0].src = URL.createObjectURL(file);
});

Try this: 尝试这个:

  $http.get(attr.imageUrl, {responseType: 'arraybuffer'})
  .then(function(response) {
    var file = new Blob([response]);
    element[0].src = URL.createObjectURL(file);
  });

Try This- 尝试这个-

$http.get(attr.imageUrl, {responseType: 'arraybuffer'})
  .then(function(response) {
    var file = new Blob([response.data], {
                    type: 'application/octet-binary'
                });
    element[0].src = URL.createObjectURL(file);
  });

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

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