dongpo5239 2013-10-07 21:47
浏览 41
已采纳

Ajax脚本仅在收到的XML足够大/需要足够长的时间才能加载时工作?

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.

This is the Ajax script:

<script>

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();
    }
  });
});

</script>

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.

Now... I've found the weirdest of problems when modifying the query: the ajax script doesn't work if the returned XML is too short??

A (simplified) example of the XML output that works fine with the Ajax script:

<results>
 <item>
  <variable1>value1</variable1>
  <variable2>value2</variable2>
  <variable3>value3</variable3>
 </item>
 <item>
  <variable1>value4</variable1>
  <variable2>value5</variable2>
  <variable3>value6</variable3>
 </item>
 <item>
  <variable1>value7</variable1>
  <variable2>value8</variable2>
  <variable3>value9</variable3>
 </item>
 <item>
  <variable1>value10</variable1>
  <variable2>value11</variable2>
  <variable3>value12</variable3>
 </item>
</results>

An example of the XML output that doesn't work:

<results>
 <item>
  <variable1>value1</variable1>
  <variable2>value2</variable2>
  <variable3>value3</variable3>
 </item>
 <item>
  <variable1>value4</variable1>
  <variable2>value5</variable2>
  <variable3>value6</variable3>
 </item>
</results>

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.

I've used Chrome debugging tools and Fiddler, and I've seen that my server is apparently returning a "500 Internal Server Errror". But, if I inspect the message in Fiddler, the correct XML output is actually there?!?!

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??

I mean, I could understand it having problems with longer datasets... but with shorter ones??

I'm completely at a loss here... any help would be really appreciated!

Edit: Some more info from Fiddler.

The ONLY difference between the response messages seems to be in the header.

Successful case:

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

Failure case:

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

Below this, both messages contain just the clean XML data as expected!

Update:

PHP code:

<?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<mysql_num_fields($result); $i++)
      $field[$i] = mysql_field_name($result, $i);

   // XML document
   $dom = new DOMDocument('1.0', 'UTF-8');
   $doc = $dom->appendChild($dom->createElement($docName));

   for ($i=0; $i<mysql_num_rows($result); $i++) {

      $node = $doc->appendChild($dom->createElement($itemName));

      for ($b=0; $b<count($field); $b++) {
         $textField = $node->appendChild($dom->createElement($field[$b]));
         $textField->appendChild($dom->createTextNode(mysql_result($result, $i, $b)));
      }
   }

   // returning XML as text
   $dom->formatOutput = true; 
   return $dom->saveXML();    
}

header ('Content-type: text/xml');
echo mysql_XML($result);

$mysql_close();

?>
  • 写回答

2条回答 默认 最新

  • douzai3399 2013-10-08 19:12
    关注

    Thanks to n.st suggestion, I found the problem!

    I looked in the server logs and I found there was a specific line in the PHP script that was throwing an error... one line that read:

    $mysqli->close();
    

    This line was supposed to be commented out (since in the end I resorted to using the deprecated 'mysql_' functions), but I had left it uncommented for some reason.

    Anyway... even though it was a PHP error, the PHP itself didn't throw any errors when accessed directly from the browser. And furthermore, this was only causing the '500 Internal Server Error' response when the query was short enough (short enough being, maybe, 10 seconds long to run), while for queries longer than that I was seeing no errors in the browser!

    Maybe for longer queries, the 500 error was somewhat "expired" by the time the actual XML arrived?? I really have no idea... but if any of you face a 500 error, remember: check for ANY errors in your PHP code! Even non-errors in the browser can become errors in the AJAX transaction.

    Thanks to all!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器