-4

Can you please let me know how I can use two Ajax call in one script and avoid conflicts? For example, I have a script like this:

<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
         type: 'POST',
         url: 'nearest.php',
         success: function( resp1 ){
          $("#div1").html(resp1);
         }
    });

      $.ajax({
         type: 'POST',
         url: 'closettime.php',
         success: function( resp2 ){
         $("#div2").html(resp2);
         }
    });   
</script>

Can you please let me know how I can use both methods in the script? Thanks.

neminem
  • 2,658
  • 5
  • 27
  • 36
Behseini
  • 6,066
  • 23
  • 78
  • 125

3 Answers3

2

Just close DOM ready with }); at the bottom of your script block and you're all set:

$(document).ready(function() {
    $.ajax({
         type: 'GET',
         url: 'nearest.php',
         success: function( resp1 ){
          $("#div1").html(resp1);
         }
    });

      $.ajax({
         type: 'GET',
         url: 'closettime.php',
         success: function( resp2 ){
         $("#div2").html(resp2);
         }
    }); 
});// <------ YOU'RE MISSING THIS

The requests should work fine. And since you're not posting any data to the URLs I might suggest that you use type:'GET'.

PeterKA
  • 24,158
  • 5
  • 26
  • 48
-1

Maybe you want to use the $.when method http://api.jquery.com/jquery.when/

jQuery.when understanding

Community
  • 1
  • 1
dobleUber
  • 566
  • 6
  • 22
  • Try to provide more detail in your answer instead of just posting multiple links – Dan Jun 09 '14 at 23:29
-2

possibly put one in the callback of the other, I don't understand the problem off the top my head. But if div2 is inside div 1 or something you can control the order in that fashion