做的项目多了我们会发现 很多时候我们需要同时调取两个甚至多个接口的数据 结果返回以后才能进行数据的处理 除了运用es6的promiseall之外 我们还可以使用jquery的$.when().done(function(){})来实现这一功能

function ajaxItem(url, params) {
    return $.ajax({
        type: "POST",
        url: url,
        // contentType: "application/json;charset=UTF-8",
        data: params,

    })
}//封装接口
//调取数据
var params1 = { id: '4dad29d22f4e44c6b21b9fddbfd38562' } //全国
var params2 = { id: '64b1abfbe9424c0b83245f9a30877359' }//分省市
$.when(
    ajaxItem(dominUrl + '/statistics/staSql/commonstajson', params1), ajaxItem(dominUrl + '/statistics/staSql/commonstajson', params2)
).done(function (res1, res2) {
    console.log(res1);//第一个接口返回值
    console.log(res2);  //第二个接口返回值
    pro(res2[0].commonsta, res1[0].commonsta)
    $('#jhlq').html(res1[0].commonsta[0].zsjhs)
    $('#dqlq').html(res1[0].commonsta[0].lqrs)
    water(toPoint(res1[0].commonsta[0].lqjd))
})

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐