javascript - Two ajax requests on same event at same time . what should be the typical behaviour? how it is different if request is synchronous -
in following javascript code, sending 2 ajax request @ same time.
after analysis using firebug, came unusual conclusion :
"which ever (ajax) response coming first printing last".
problem 2: if assign ajax url destination random string (say "abcd") [which don't exist] total number of ajax call increased 3?
$(document).ready(function(e) { $("form[ajax=true]").submit(function(e) { e.preventdefault(); var form_data = $(this).serialize(); var form_url = $(this).attr("action"); var form_method = $(this).attr("method").touppercase(); $("#loadingimg").show(); $.ajax({ url: form_url, type: form_method, data: form_data, cache: false, success: function(returnhtml){ alert ("a"); // $("#result").html(returnhtml); // $("#loadingimg").hide(); } }); $.ajax({ url: form_url, type: form_method, data: form_data, cache: false, success: function(returnhtml){ // $("#duplicate").html(returnhtml); // $("#loadingimg").hide(); alert("b"); } }); }); });
please refer following fiddle.
gaurav, have error, @ end of 1st $.ajax must end ),
, 2nd )
.
you can't end ;
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); });
Comments
Post a Comment