如何使用两个$ .post方法很好地从2个不同的数据库中提供数据

 function ChangeGallery(){
    var GalleryName = $('.SubSubGalleryLock').text();

    /*Send string to Data1.php and include Tags from Database*/
    $.post("Data1.php", {  Sections: GalleryName  },
    function(data){
         $(".IncludeData").append(data);
    }); 

    /*send string to Data2.php and include Events data from Database*/
    $.post("Data2.php",{  GallerySec: GalleryName  },
    function(response){
         /*when i use alert method, this function works very well, why?*/
         alert('SomeString');
     var data = jQuery.parseJSON(response);
         var ImageID  = data[0];
         var ImageSrc = data[1];
     $(ImageID).click(function(){
        $(".LargeImage").attr('src', ImageSrc);
     });
     });
};

in Data1.php

  /*give data from database1 and print to HTML File*/
  if ($_POST['Sections']) == "String")
  {  $results = mysql_query("SELECT * FROM Table1");
  while($row = mysql_fetch_array($results))
  { echo $row['Tags']; }

in Data2.php

  /*give data from database2 and Use for events*/
  if ($_POST['GallerySec']) == "String")
  {  $results = mysql_query("SELECT * FROM Table2");
  while($row = mysql_fetch_array($results))
  { echo json_encode($row); }

in Client side when i use it then Data1.php works very well but Data2.php only when i write an

alert('Some stringh');

after

var data = jQuery.parseJSON(response); 

line, it's work well, why? what's due to this problem?

I'm going to guess that you need the second .post() to be processed AFTER the first .post() and when you put the alert() in, that guarantees that it will go in that order, but without the alert(), it is a race condition and depends upon which .post() returns quicker.

There are a couple ways to fix the sequencing. The most straightforward is to start the second .post() from the success handler of the first so that you know the first has already completed.

You can also use jQuery promises or you could use your own flags to keep track of which has finished and call the last bit of code only when both have finished.

Here's how you would start the second .post() from the success handler of the first to guarantee the order:

function ChangeGallery(){
    var GalleryName = $('.SubSubGalleryLock').text();

    /*Send string to Data1.php and include Tags from Database*/
    $.post("Data1.php", {  Sections: GalleryName  },
    function(data){
         $(".IncludeData").append(data);
        /*send string to Data2.php and include Events data from Database*/
        $.post("Data2.php",{  GallerySec: GalleryName  },
            function(response){
                var data = jQuery.parseJSON(response);
                var ImageID  = data[0];
                var ImageSrc = data[1];
                $(ImageID).click(function(){
                    $(".LargeImage").attr('src', ImageSrc);
                });
         }); 
     });
};