Parallel Ajax Calls in Javascript/jQuery

前端 未结 9 2280
甜味超标
甜味超标 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:56

    Let's try to do it in this way:

    
    

    if server side will be smth. like this:

    public function url0() {
        $answer = array(
            array('smth' => 1, 'ope' => 'one'),
            array('smth' => 8, 'ope' => 'two'),
            array('smth' => 5, 'ope' => 'three')
        );
        die(json_encode($answer));
    }
    
    public function url1() {
        $answer = array('one','two','three');
        die(json_encode($answer));
    }
    
    public function url2() {
        $answer = 'one ,two, three';
        die(json_encode($answer));
    }
    

    So there, as you can see, created one function getData() for getting data from server and than it called 3 times. Results will be received in asynchronous way so, for example, first can get answer for third call and last for first call.

    Console answer will be:

    [{"smth":1,"ope":"one"},{"smth":8,"ope":"two"},{"smth":5,"ope":"three"}]
    
    ["one","two","three"]
    
    "one ,two, three"
    

    PS. please read this: http://api.jquery.com/jQuery.ajax/ there you can clearly see info about async. There default async param value = true.

    By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active...

提交回复
热议问题