Javascript jquery多个http get请求
我正在尝试使用jqueryget从服务器获取多个图像Javascript jquery多个http get请求,javascript,jquery,promise,Javascript,Jquery,Promise,我正在尝试使用jqueryget从服务器获取多个图像 $.each(result.Data.Files, function (i, v) { $.get('/mobile/image?path=' + v, function (res) { if (res.Success) { $('#images-container').append(`<img alt="Image Title" src="${res.Ima
$.each(result.Data.Files, function (i, v) {
$.get('/mobile/image?path=' + v, function (res) {
if (res.Success) {
$('#images-container').append(`<img alt="Image Title" src="${res.Image}">`);
}
});
});
在附加所有图像之后,我想调用jquery插件来处理这些图像。
我怎样才能用承诺做到这一点?
我尝试了一些类似于:
$.when(
$.each(result.Data.Files, function (i, v) {
$.get('/mobile/image?path=' + v);
})
).then(function () {
$("#images-container").image_plugin();
for (var i = 0; i < arguments.length; i++) {
let src = arguments[i];
console.log(src)
}
});
但是我在then部分得到的只是URL,而不是实际的base64响应
谢谢使用
$。每个都不返回任何内容。所以你的$。什么时候都不做。改用map并返回由$.getdefs[]=$.Deferred创建的承诺;此行抛出一个异常。你是什么意思?哎呀…今天php太多了…修复了
var defs = [];
//create Deferred for each item in array and push to defs array
$.each(result.Data.Files, function () {
defs.push($.Deferred());
});
$.when.apply($, defs).then( function(v){
//runs when all deferred are resolved
$("#images-container").image_plugin();
console.log(v)
});
$.each(result.Data.Files, function (i, v) {
$.get('/mobile/image?path=' + v, function (res) {
if (res.Success) {
$('#images-container').append(`<img alt="Image Title" src="${res.Image}">`);
defs[i].resolve(res.Image);
//resolve i'th deferred
}
});
});