对使用 Ajax 捕获的动态 url 响应

Dynamic url response to captured using Ajax

本文关键字:动态 url 响应 Ajax      更新时间:2023-09-26
var urlArray = ["api/vehicle?sttime=1424044800&endtime=1424390400","api/vehicle?type=travel&stt‌​ime=1424476800&endtime=1424822400","api/vehicle/?type=travel&sttime=1424908800&en‌​dtime=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
 }

您可以使用参数对象,它是一种特殊类型的数组,类似于对象,它具有传递给该方法的所有参数的索引列表。

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)
    })
}

演示:小提琴