title: Calling ajax inside ajax
body: <p>This is my first day and first question here, hope you will forgive me if my question is very trivial for this platform.</p>
  
  <p>I am trying to call ajax inside ajax, One ajax call is going to call one cotroller action in which it will insert a record in the database, The action for the 1st ajax call is </p>
  
  <pre><code>public function createAction(Request $request){
      if ($request-&gt;isXmlHttpRequest()) {
          $name = $request-&gt;get("gname");
          $description = $request-&gt;get("desc");
                      $portfolio_id = $request-&gt;get("PID");
                      $portfolio = $this-&gt;getDoctrine()
                          -&gt;getRepository('MunichInnovationGroupPatentBundle:PmPortfolios')
                          -&gt;find($portfolio_id);
          $portfolio_group = new PmPatentgroups();
          $portfolio_group-&gt;setName($name);
          $portfolio_group-&gt;setDescription($description);
          $portfolio_group-&gt;setPortfolio($portfolio);
          $portfolio_group-&gt;setOrder(1000000);
          $portfolio_group-&gt;setIs_deleted(0);
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $em-&gt;persist($portfolio_group);
          $em-&gt;flush();
          $msg = 'true';
      }
      echo $msg;
      return new Response();
  }
  </code></pre>
  
  <p>The 2nd ajax call is going to get the updated data that is inserted by the first ajax call, The action for this call is</p>
  
  <pre><code>public function getgroupsAction(Request $request){
      if ($request-&gt;isXmlHttpRequest()) {
  
          $id = $request-&gt;get("PID");
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $portfolio_groups = $em-&gt;getRepository('MunichInnovationGroupPatentBundle:PmPatentgroups')
          -&gt;getpatentgroups($id);
          echo json_encode($portfolio_groups);
          return new Response();
      }
  }
  </code></pre>
  
  <p>My JQuery is as follows</p>
  
  <pre><code> $.ajax({
           type: 'POST',
           url: url,
           data: data,
           success: function(data) {
           if(data == "true") {
                   $("#new-group").fadeOut("fast", function(){
                   $(this).before("&lt;strong&gt;Success! Your Portfolio Group is created Successfully.&lt;/strong&gt;");
                   setTimeout("$.fancybox.close()", 3000);
                   });
                    $.ajax({
                            type: 'POST',
                            url: getgroups,
                            data: data,
                            success: function(data) 
                            { 
                              var myArray = JSON.parse(data); 
                              var options = $("#portfolio-groups"); 
                              for(var i = 0; i &lt; myArray.length; i++)
                              { 
                                options.append($("&lt;option /&gt;").val(myArray[i].id).text(myArray[i].name)); 
                               } 
                         } 
                  });
              }
           }
       });
  </code></pre>
  
  <p>I am calling the 2nd ajax inside the success of the 1st one to ensure that the first ajax is successfully completed, but the 2nd ajax call is not getting the updated data.</p>
  
  <p>How can I ensure that the 2nd ajax will be called after the completion of the first one and I get the recently inserted data as well</p>
  
  <p>Thanks</p>
  
  <p><strong>MY SOLUTION</strong>
  Just using one ajax call</p>
  
  <p>in the create action where an insertion is made , just after the insertion take all the groups for the portfolio, and return json_encode($portfolio_groups);</p>
  
  <p>Inside the JQuery</p>
  
  <pre><code> $.ajax({
          type: 'POST',
          url: url,
          data: data,
          success: function(data) {
                $("#new-group").fadeOut("fast", function(){
                $(this).before("&lt;strong&gt;Success! Your Portfolio Group is created Successfully.&lt;/strong&gt;");
                setTimeout("$.fancybox.close()", 3000);
                });
                var myArray = JSON.parse(data); 
                var options = $("#portfolio-groups"); 
                for(var i = 0; i &lt; myArray.length; i++)
                { 
                   options.append($("&lt;option /&gt;").val(myArray[i].id).text(myArray[i].name)); 
                 } 
              }
         });   
  </code></pre>