making ajax request and waiting for file

making ajax request and waiting for file

I have a click function that almost works, but it seems I have misunderstood how the ajax request functions. I want to post a string and then wait for a file which I will process when it arrives in the directory. My script works if the file already exists, but not if I click and then add the file.  What am I missing? Thanks for any assistance

  1.   <script>
               //post NLQ string    
               $(document).ready(function(){
               var nlqString = '';
               $('#nlq-execute').click(function(){
               nlqString = $('#nlq-string').val();
               console.log(nlqString); //for debugging
               $.post('test.jsp',{q:nlqString});
               
               // begin ajax request
               $.ajax({
                   type:'get',
                   url:'madsat-data.xml',
                   beforeSend: function(){$('#query-results').html('<img src="images/ajax-loader.gif"><br>Loading...'); },
                   timeout: 10000,
                   error: function(xhr, status, error){
                    alert("Error: " + xhr.status + " - " + error)
                   },
                   dataType: "xml",
                   success: function(data){
                    
                   var html = '<h3>Query Results</h3>';
                   html += '<table><tbody><tr>';
                   
                   var keys = $(data).find('row:first>*').map(function(){return this.nodeName}).get();
                   console.log(keys);
                   console.log(keys.length);
                   for (i=0; i<keys.length; i++){
                   
                   html += '<th class="' + keys[i] +'">' + keys[i] + '</th>'
                   }
                html += '</tr>';    
                   
                    $(data).find('row').each(function(){
                        var row = $(this);
                        
                        html+= '<tr>';
                        $(data).find(row).children().each(function(){
                        
                        html+= '<td>' + $(this).text() + '</td>';
                        
                            }); //end each column
                        
                        html+= '</tr>'
                        });  //end each row
                                 
                    
                   
                
                
                
            $('#query-results').html(html); // Add to DOM
            
                    
                   }//end success
                
        
                
                                  
               });//end ajax
               
               }); // end click
               
            });   //end document ready