Use multiple URLs with $.getJSON

后端 未结 2 1455
小蘑菇
小蘑菇 2020-12-06 21:22

I have a few lines of code:

var url = \'http://api.spitcast.com/api/spot/forecast/1/\';
var url_wind = \'http://api.spitcast.com/api/county/wind/orange-count         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 22:17

    you cannot, use two separate calls:

    $.getJSON(url, function (data) {
        $.getJSON(url_wind, function (data2) {
            //do stuff with 'data' and 'data2'
        });    
    });
    

    Example above will execute the second call (to url_wind) when the first call (to url) has finished. To execute the two calls in parallel, use $.when() like this:

    $.when($.getJSON(url), $.getJSON(url_wind)).done(function(data1, data2) {
        //do stuff with 'data' and 'data2'
    });
    

提交回复
热议问题