Parallel Ajax Calls in Javascript/jQuery

前端 未结 9 2282
甜味超标
甜味超标 2021-02-07 17:26

I am completely new to Javascript/jquery world and need some help. Right now, I am writing one html page where I have to make 5 different Ajax calls to get the data to plot grap

9条回答
  •  無奈伤痛
    2021-02-07 17:54

    You won't be able to handle it like your example. Setting to async uses another thread to make the request on and lets your application continue.

    In this case you should utilize a new function that will plot an area out, then use the callback functions of the ajax request to pass the data to that function.

    For example:

    $(document).ready(function() {
        function plotArea(data, status, jqXHR) {
          // access the graph object and apply the data.
          var area_data = $.parseJSON(data);
        }
    
        $.ajax({
            url : url0,
            async : false,
            dataType : 'json',
            success: poltArea
        });
    
        $.ajax({
            url : url1,
            async : false,
            dataType : 'json',
            success: poltArea
        });
    
        $.ajax({
            url : url4,
            async : false,
            dataType : 'json',
            success: poltArea
        });
    
        // some code for generating graphs
    
    }); // closing the document ready function 
    

提交回复
热议问题