var urlArray = ["api/vehicle?sttime=1424044800&endtime=1424390400","api/vehicle?type=travel&sttime=1424476800&endtime=1424822400","api/vehicle/?type=travel&sttime=1424908800&endtime=1425859199"]
function succ(eachurl) {
return _.http.get(eachurl);
//calling ajax using _.http.get(url)
}
var ps = _.map(urlArray, succ);
//and ps will be [{"readyState":1},{"readyState":1},{"readyState":1}]
$.when.apply($, ps).always(alwaysFun);
每当我调用 $.when 函数时,函数将根据动态的数组大小来命中服务器并调用。
function alwaysFun(res1, res2, res3) {
//works fine prints all the three responses from 3 urls
console.log(res1);
console.log(res2);
console.log(res3);
}
但我的疑问是数组是否是动态的包含 10 个 url [A, B ...Z] 并且响应也将有 10 个响应,如果命中的是动态 url 数组,我如何在回调函数中捕获它。 这是我的动态数组 url 的方法;
ps = [A, b...z];
$.when.apply($, ps).always(alwaysFun);
//How can i will mention dynamic responses in call back please
function alwaysFun(res1, res2 ....resZ) {
//Please guide on this issue
}
最佳答案
您可以使用arguments object ,它是一种特殊类型的类似数组的对象,它具有传递给该方法的所有参数的索引列表。
function alwaysFun(res1, res2, res3) {
//works fine prints all the three responses from 3 urls
console.log(res1);
console.log(res2);
console.log(res3);
//user
console.log('using arguments')
$.each(arguments, function (i, arg) {
console.log(arg)
})
}
演示:Fiddle
关于javascript - 使用 Ajax 捕获的动态 url 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28936881/