我正在尝试通过以下代码通过 ajax
加载图像,该代码已经在运行。我试图将它转换成 Angular $http
但它不起作用。
纯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 代码
$http.get(attr.imageUrl, {}, {
responseType: 'arraybuffer'
})
.success(function(response) {
var file = new Blob([response]);
element[0].src = URL.createObjectURL(file);
});
最佳答案
我认为应该是:
$http.get({ url: attr.imageUrl, responseType: 'arraybuffer'})
.success(function(response) {
var file = new Blob([response]);
element[0].src = URL.createObjectURL(file);
});
或者:
$http.get(attr.imageUrl, {responseType: 'arraybuffer'})
.success(function(response) {
var file = new Blob([response]);
element[0].src = URL.createObjectURL(file);
});
关于javascript - 如何将 xhr 请求转换为 Angular $http 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35838275/