title: Ajax script only works when the received XML is big enough/takes long enough to load?
body: <p>I have a jQuery script that calls a PHP file that will query a database, then spit the results as XML. The jQuery script takes the XML, parses it and puts it inside an array to draw some charts.</p>
  
  <p>This is the Ajax script:</p>
  
  <pre><code>&lt;script&gt;
  
  var arr={};    
  
  $(document).ready(function(){
  
    $.ajax({
      type: "POST",
      url: "http://myserver.com/query.php",
      dataType: "xml",
      success: function(xml) {
          $(xml).find('item').each(function(index,value){
              var data={};
              $(this).find('*').each(function(i,v){
                  if(isNaN(parseFloat($(this).text()))) {
                      data[$(this).prop('tagName')]=$(this).text();
                  } else {
                      data[$(this).prop('tagName')]=parseFloat($(this).text());
                  }
              });
              arr[index]=data;
          })
          DataLoaded();
      }
    });
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>I've tested this script using a real query inside the query.php file, and it works fine. Data is retrieved from the DB, shaped into XML format and sent back to JavaScript, where it's correctly processed and played with.</p>
  
  <p>Now... I've found the weirdest of problems when modifying the query: the ajax script doesn't work if the returned XML is <strong>too short??</strong></p>
  
  <p>A (simplified) example of the XML output that works fine with the Ajax script:</p>
  
  <pre><code>&lt;results&gt;
   &lt;item&gt;
    &lt;variable1&gt;value1&lt;/variable1&gt;
    &lt;variable2&gt;value2&lt;/variable2&gt;
    &lt;variable3&gt;value3&lt;/variable3&gt;
   &lt;/item&gt;
   &lt;item&gt;
    &lt;variable1&gt;value4&lt;/variable1&gt;
    &lt;variable2&gt;value5&lt;/variable2&gt;
    &lt;variable3&gt;value6&lt;/variable3&gt;
   &lt;/item&gt;
   &lt;item&gt;
    &lt;variable1&gt;value7&lt;/variable1&gt;
    &lt;variable2&gt;value8&lt;/variable2&gt;
    &lt;variable3&gt;value9&lt;/variable3&gt;
   &lt;/item&gt;
   &lt;item&gt;
    &lt;variable1&gt;value10&lt;/variable1&gt;
    &lt;variable2&gt;value11&lt;/variable2&gt;
    &lt;variable3&gt;value12&lt;/variable3&gt;
   &lt;/item&gt;
  &lt;/results&gt;
  </code></pre>
  
  <p>An example of the XML output that <strong>doesn't</strong> work:</p>
  
  <pre><code>&lt;results&gt;
   &lt;item&gt;
    &lt;variable1&gt;value1&lt;/variable1&gt;
    &lt;variable2&gt;value2&lt;/variable2&gt;
    &lt;variable3&gt;value3&lt;/variable3&gt;
   &lt;/item&gt;
   &lt;item&gt;
    &lt;variable1&gt;value4&lt;/variable1&gt;
    &lt;variable2&gt;value5&lt;/variable2&gt;
    &lt;variable3&gt;value6&lt;/variable3&gt;
   &lt;/item&gt;
  &lt;/results&gt;
  </code></pre>
  
  <p>For the last case, the PHP is working fine (I can access the PHP file directly from the browser and I see the XML output as shown above), but if I run the HTML file on the server, it'll simply stall forever without ever showing the data.</p>
  
  <p>I've used Chrome debugging tools and Fiddler, and I've seen that my server is apparently returning a "<strong>500 Internal Server Errror</strong>". But, if I inspect the message in Fiddler, the correct XML output is actually there?!?!</p>
  
  <p>Why is the server returning a 500 Internal Server Error together with the XML data?? And why is it throwing the error ONLY when the output is too short??</p>
  
  <p>I mean, I could understand it having problems with longer datasets... but with <em>shorter</em> ones??</p>
  
  <p>I'm completely at a loss here... any help would be really appreciated!</p>
  
  <p><strong>Edit:</strong>
  Some more info from Fiddler.</p>
  
  <p>The ONLY difference between the response messages seems to be in the header.</p>
  
  <p>Successful case:</p>
  
  <pre><code>HTTP/1.1 200 OK
  Date: Mon, 07 Oct 2013 21:50:10 GMT
  Server: Apache/2.2.12 (Linux/SUSE)
  X-Powered-By: PHP/5.2.14
  Content-Length: 4627
  Keep-Alive: timeout=15, max=99
  Connection: Keep-Alive
  Content-Type: text/xml
  </code></pre>
  
  <p>Failure case:</p>
  
  <pre><code>HTTP/1.0 500 Internal Server Error
  Date: Mon, 07 Oct 2013 21:21:22 GMT
  Server: Apache/2.2.12 (Linux/SUSE)
  X-Powered-By: PHP/5.2.14
  Content-Length: 371
  Connection: close
  Content-Type: text/xml
  </code></pre>
  
  <p>Below this, both messages contain just the clean XML data as expected!</p>
  
  <p><strong>Update:</strong></p>
  
  <p><strong>PHP code:</strong></p>
  
  <pre><code>&lt;?php
  
  $dbhost = 'myserver.com';
  $dbuser = 'login';
  $dbpass = 'password';
  $dbname = 'DB';
  
  
  $link = mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error"); 
  mysql_select_db($dbname, $link) or die("DB error");
  
  
  $sql = "select bla bla bla... working query here";
  
  $result = mysql_query($sql);
  
  function mysql_XML($result, $docName='results', $itemName='item') {
     $field = array();
  
     for ($i=0; $i&lt;mysql_num_fields($result); $i++)
        $field[$i] = mysql_field_name($result, $i);
  
     // XML document
     $dom = new DOMDocument('1.0', 'UTF-8');
     $doc = $dom-&gt;appendChild($dom-&gt;createElement($docName));
  
     for ($i=0; $i&lt;mysql_num_rows($result); $i++) {
  
        $node = $doc-&gt;appendChild($dom-&gt;createElement($itemName));
  
        for ($b=0; $b&lt;count($field); $b++) {
           $textField = $node-&gt;appendChild($dom-&gt;createElement($field[$b]));
           $textField-&gt;appendChild($dom-&gt;createTextNode(mysql_result($result, $i, $b)));
        }
     }
  
     // returning XML as text
     $dom-&gt;formatOutput = true; 
     return $dom-&gt;saveXML();    
  }
  
  header ('Content-type: text/xml');
  echo mysql_XML($result);
  
  $mysql_close();
  
  ?&gt;
  </code></pre>