Use multiple URLs with $.getJSON

后端 未结 2 1456
小蘑菇
小蘑菇 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:16

    You'll need two calls, but you can use $.when to tie them to the same done() handler :

    var url = 'http://api.spitcast.com/api/spot/forecast/1/';
    var url_wind = 'http://api.spitcast.com/api/county/wind/orange-county/';
    
    $.when(
        $.getJSON(url),
        $.getJSON(url_wind)
    ).done(function(result1, result2) {
    
    });
    
    0 讨论(0)
  • 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'
    });
    
    0 讨论(0)
提交回复
热议问题