title: Nested AJAX Calls
body: <p>in first ajax call server returns a json string that is converted to js array. I want to run an ajax call for each element of array.</p>
  
  <p>For example, first ajax call returns semester 1,2,3,4. Now i want to retrieve subjects for semester 1 and show them in a table, then retrieve subjects of semester 2 and show all subjects in a table and so on...</p>
  
  <p>I wrote a code but it shows all subjects in last table. Can some one help me?</p>
  
  <pre><code>$.ajax(
         {
             type: "POST",
             url: "returnSemesterNumbers",
             data: "programId="+selectedprog,
             success: function(data)
             {
                  var semesters=$.parseJSON(data);
                  for(x in semesters)
                  {
                      semnum=semesters[x].semesterNumber;
                      alert(semnum);
                      $("#tables").append('&lt;table id=table'+x+' style=float:left&gt;&lt;tr&gt;&lt;td&gt;'+semnum+'&lt;/td&gt;&lt;/tr&gt;');
                      $.ajax(
                             {
                                 type: "POST",
                                 url: "returnCourses",
                                 data: "programId="+selectedprog+"&amp;semNum="+semnum,
                                 success: function(data1)
                                 {
                                     var courses=$.parseJSON(data1);
                                      for(y in courses)
                                      {
                                          $("#table"+x).append('&lt;tr&gt;&lt;td&gt;'+courses[y].title+'&lt;/td&gt;&lt;/tr&gt;');
                                      }
                                 }
                               });
                      $("#table"+x).append('&lt;/table&gt;');
                      }
                 }
               });
              }
              else
              {
                  $("#tables").css('display','none');
              }
  });
  </code></pre>