喵-见缝插针 2016-05-09 05:03 采纳率: 0%
浏览 59

在一个ajax中调用多个api

For example I have a ajax function like below

$.ajax({ 
            url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"119",//getting the api
            type: 'get',
            success: function(data){
                console.log(data.result);
            }
            });

in above it only calls the profile 119 but i'd like to call 118 and 177 too, i'd like to know how to do it in a for loop, because in reality it the number of api we need to call will be unknown

url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"118",
url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"177",

The below is INCORRECT method i just want to give out my think

     $.ajax({ 
                    url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"119",
 '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"118",'/dashboard/getTrend'+'?period=30d' +"&profileId=" +"177",
//getting the api
                    type: 'get',
                    success: function(data){
                        console.log(data.result);
                    }
                    });

UPDATE 1:

//trend chart
        function trend1(){
        $.ajax({ 
        url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"119",//getting the api
        type: 'get',
        success: function(data){
            console.log(data.result) //correct
        }
        });
        }
        function trend2(){
        $.ajax({ 
        url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"120",//getting the api
        type: 'get',
        success: function(data){
            console.log(data.result) //correct
        }
        });
        }

        $.when( trend1(), trend2()).done(function(trend1_data, trend_data){

            console.log(trend1_data.result)
            console.log(trend2_data.result)
        });

i tried to do this is this correct?

UPDATE 2:

function trend1() {
            return $.ajax({
                url: '/dashboard/getTrend' + '?period=30d' + "&profileId=" + "119", //getting the api
                type: 'get',
                success: function(data) {
                    console.log(data.result)
                }
            });
        }

        function trend2() {
            return  $.ajax({
                url: '/dashboard/getTrend' + '?period=30d' + "&profileId=" + "120", //getting the api
                type: 'get',
                success: function(data) {
                    console.log(data.result)
                }
            });
        }

        $.when(trend1(), trend2()).done(function(trend1_data, trend2_data) {
            console.log(trend1_data.result) //undefined
            console.log(trend2_data.result)//undefined
        });

I tried to modify the function to this but the console.log() inside the when function are not defined, which means it read nothing.

  • 写回答

2条回答 默认 最新

  • weixin_33724659 2016-05-09 05:12
    关注

    You can't pass multiple URL to $.ajax() method, However jQuery.when() can be used.

    $.when( 
        $.ajax( {url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"119", method:"GET"}), 
        $.ajax( {url: '/dashboard/getTrend'+'?period=30d' +"&profileId=" +"177", method:"GET"}),
    )
    .done(function( a1, a2 ) {
       // a1 and a2 are arguments resolved for the url1 and url2 ajax requests, respectively.
       // Each argument is an array with the following structure: [ data, statusText, jqXHR ]
    });
    

    You need to return the promise from your trend1 and trend2 method

    //trend chart
    function trend1() {
        return $.ajax({
            url: '/dashboard/getTrend' + '?period=30d' + "&profileId=" + "119", //getting the api
            type: 'get',
            success: function(data) {
                console.log(data.result)
            }
        });
    }
    
    function trend2() {
        return  $.ajax({
            url: '/dashboard/getTrend' + '?period=30d' + "&profileId=" + "120", //getting the api
            type: 'get',
            success: function(data) {
    
            }
        });
    }
    
    $.when(trend1(), trend2()).done(function(trend1_data, trend_data) {
        console.log(trend1_data.result)
        console.log(trend2_data.result)
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用