php - Ajax 和 Internet Explorer

标签 php javascript xml ajax

我有一段带有 ajax 请求的 JavaScript 代码,该代码打印出在 IE8 中不起作用的 XML 文件的内容。 当我用谷歌搜索这个问题时,我遇到了一些建议,并且我尝试了那些似乎与我想做的事情相关的建议。我将以下行添加到我的 .htaccess 文件中: AddType application/xml .xml .rss;我将 overridemimetype 添加到代码中,并在 xml 文件的开头添加了以下行: header('Content-type: text/xml')。这些都对 IE8 中的输出没有任何影响,但将内容类型行添加到 XML 文件会阻止它在 Firefox 中工作。我认为我一定误解了有关内容类型行的内容,因此我尝试将其包含在创建 XML 文件的 php 文件中。当我将它放在 PHP 文件的开头时,我收到此警告:警告:无法修改 header 信息 - header 已发送。我尝试将其放在创建 XML 文件的函数的开头,但这没有任何效果。我正在使用的代码如下

<html>
<head>
<body>

    <script type ="text/javascript">

        if (window.XMLHttpRequest)
        {
            xhttp=new XMLHttpRequest();
        }
        else // 
        {
            xhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xhttp.overrideMimeType)
            xhttp.overrideMimeType('text/xml')


        xhttp.open("GET","captions_7.xml",false);
        xhttp.send();
        xmlDoc=xhttp.responseXML;
        if(xmlDoc)
        {
            document.write("ok");
            //in ie ok prints

        }
        else
        {
            document.write("failed");
        }

        elements = xmlDoc.getElementsByTagName("p");
        length = elements.length;

        for (var i=0; i<length ; i++)
        {
            //caption text
            document.write(xmlDoc.getElementsByTagName("p")[i].childNodes[0].nodeValue + "<br />");
            //start time
            document.write(xmlDoc.getElementsByTagName("p")[i].getAttribute("begin")+ "<br />");
            //end time
            document.write(xmlDoc.getElementsByTagName("p")[i].getAttribute("end")+ "<br />");
        }


    </script>

</body>
</html>

在 IE8 中,该文件的唯一输出是正常的。在 Firefox、Opera 和 Chrome 中,显示 XML 文件的内容。我已经在验证器中检查了 XML 文件,没有报告任何错误,所以我认为这不是问题所在。如果有人知道此问题的解决方案或对我可以尝试的内容有任何建议,我将不胜感激。

如果它很重要,这是创建 xml 文件的函数:

function createXML($referenceId){
header('Content-type: text/xml');
$content_id = $referenceId;
$XMLStart="<tt xml:lang=\"en\"><head><layout/></head><body><div xml:id=\"captions\">";
$XMLEnd = "</div></body></tt>";

$text;


$captionQuery = "SELECT caption_text, start_time, end_time FROM captions WHERE content_id = $content_id ORDER BY start_time ASC;";

$captionResult = mysql_query($captionQuery);


if(mysql_num_rows($captionResult)){

    while($row = mysql_fetch_array($captionResult)){


        $text.="<p begin=\"".$row[start_time]."\" end=\"".$row[end_time]."\">".$row[caption_text]."</p>";

         }
}//end if

$XMLString = $XMLStart.$text.$XMLEnd;

$captionsFile="captions_".$content_id.".xml";
$captionsFileHandle = fopen($captionsFile, w) or die("can't open file");
fwrite($captionsFileHandle, $XMLString);
fclose($captionsFileHandle);
return $captionsFile;  

}

我对这一切都很陌生,所以如果这是一个非常愚蠢的问题,我深表歉意。我认为这必须每天完成数千次,但我只是不知道我能做些什么来解决它。

最佳答案

我强烈推荐使用jQuery的AJAX功能;它是跨浏览器兼容的,因此应该有助于解决问题。

关于php - Ajax 和 Internet Explorer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5887897/

相关文章:

php - 在 Python 中使用 flask 进行 Hmac 验证(在 PHP 和 RUBY 中有引用)

javascript - 试图从字符串中删除 'px' 吗?

javascript - SAPui5 创建简单的 sap.m.ComboBox

css - 分页问题

java - 如何在 Spring XML 配置中设置 LocalDateTime

php - 处理数百万文档的技巧?

php - 出于角色目的,在每个 http 请求上使用现有表名创建 mysql View

c++ - 解析 xml 时无法获取属性的名称和值

php - Wordpress:如果我想从 javascript 文件调用我的 php 脚本,该把它放在哪里?

javascript - Ajax刷新了php但没有更新