Two ajax requests on same event at same time . what should be the typical behaviour? how it is different if request is synchronous

后端 未结 4 2027
轻奢々
轻奢々 2020-12-31 20:40

In the following javascript code, I am sending two Ajax request at the same time.
After analysis using Firebug, I came to unusual conclusion that :
\"which ever

相关标签:
4条回答
  • 2020-12-31 20:49

    Gaurav, you have an error, at the end of the 1st $.ajax it must end as ), and 2nd as ).

    You can't end with ;

    var result1;
    var result2;
    $.when(
        $.ajax({ // First Request
            url: form_url, 
            type: form_method,      
            data: form_data,     
            cache: false,
            success: function(returnhtml){     
                    result1 = returnhtml;                  
            }           
        }),
    
        $.ajax({ //Seconds Request
            url: form_url, 
            type: form_method,      
            data: form_data,     
            cache: false,
            success: function(returnhtml){                          
                result2 = returnhtml;     
            }           
        })
    
    ).then(function() {
        $('#result1').html(result1);
        $('#result2').html(result2);
    });

    0 讨论(0)
  • 2020-12-31 21:02

    I'm not sure I completely understand, but I will try to give you some information. Like David said It may seem that the first request is the last one responding, but that will vary under many circumstances. There are different ways you could do this to control the outcome or order of the requests.

    1) Upon success of the first request you could initiate the second request. I don't recommend this for speed purposes as your requests aren't running in parallel.

    $.ajax({ // First Request
        url: form_url, 
        type: form_method,      
        data: form_data,     
        cache: false,
        success: function(returnhtml){     
            $.ajax({ //Seconds Request
                url: form_url, 
                type: form_method,      
                data: form_data,     
                cache: false,
                success: function(returnhtml){                          
                   // $("#duplicate").html(returnhtml); 
                   // $("#loadingimg").hide();
                    alert("b");
                }           
            }); 
           alert ("a");
           // $("#result").html(returnhtml); 
           // $("#loadingimg").hide();                    
           }           
        });   
    

    2) If you need to have both requests responses at the same time, the preferred method would likely be jQuery deferred. This will make both requests run in parallel, and once both responses are received you can proceed as you would have.

    Something Like this:

    var result1;
    var result2;
    $.when(
        $.ajax({ // First Request
            url: form_url, 
            type: form_method,      
            data: form_data,     
            cache: false,
            success: function(returnhtml){     
                    result1 = returnhtml;                  
            }           
        }); 
    
        $.ajax({ //Seconds Request
            url: form_url, 
            type: form_method,      
            data: form_data,     
            cache: false,
            success: function(returnhtml){                          
                result2 = returnhtml;     
            }           
        }); 
    
    ).then(function() {
        $('#result1').html(result1);
        $('#result2').html(result2);
    });
    

    Check out:

    https://api.jquery.com/jQuery.when/

    http://api.jquery.com/deferred.then/

    https://api.jquery.com/deferred.done/

    I hope this helps!

    0 讨论(0)
  • 2020-12-31 21:04

    Or use server_response in your code. The script begin with condition:

    if (recherche1.length>1) {
        $.ajax({ // First Request
            type :"GET",
            url : "result.php",
            data: data,     
            cache: false,
            success: function(server_response){     
                $('.price1').html(server_response).show();                  
            }           
        }),
    
        $.ajax({ //Seconds Request
            type :"GET",
            url : "result2.php",
            data: data,     
            cache: false,
            success: function(server_response){                          
                $('.price2').html(server_response).show();      
            }           
        });
    }
    
    0 讨论(0)
  • 2020-12-31 21:12
    var result1;
    var result2;
    $.when(
        $.ajax({ // First Request
            url: form_url, 
            type: form_method,      
            data: form_data,     
            cache: false,
            success: function(returnhtml){     
                    result1 = returnhtml;                  
            }           
        }); 
    
        $.ajax({ //Seconds Request
            url: form_url, 
            type: form_method,      
            data: form_data,     
            cache: false,
            success: function(returnhtml){                          
                result2 = returnhtml;     
            }           
        }); 
    
    ).then(function() {
        $('#result1').html(result1);
        $('#result2').html(result2);
    });
    
    0 讨论(0)
提交回复
热议问题