简体   繁体   中英

Ajax and internet explorer

I have a piece of JavaScript code with an ajax request that prints out the contents of an XML file that won't work in IE8. I came across some suggestions when I googled this problem and I've tried those that seemed relevant to what I was trying to do. I added the following line to my .htaccess file: AddType application/xml.xml.rss; I added overridemimetype to my code and I included the following line at the beginning of my xml file: header('Content-type: text/xml'). None of these have made any difference to the output in IE8 but adding the content type line to the XML file stopped it working in Firefox. I thought I must be misunderstanding something about the content type line so I tried including it in the php file that creates the XML file. When I put it at the beginning of the PHP file I got this warning: Warning: Cannot modify header information - headers already sent. I tried putting at the beginning of the function that creates the XML file but that didn't have any effect. The code I'm using is below

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

In IE8 the only output from this file is OK. In Firefox, Opera and Chrome, the contents of the XML file are displayed. I've checked the XML file in a validator and it reported no errors, so I don't think that's the problem. I would really appreciate it if anyone knows of a solution to this or has any suggestions as to what I could try.

In case it's important, this is the function that creates the xml file:

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;  

}

I'm very new to all this so apologies if this is a very stupid question. I assume this must be done everyday many thousands of times but I just can't see what I can do to fix it.

I highly recommend using jQuery's AJAX function; it's cross-browser compatible, so it should help clear up the issue.

Your PHP function is doing this: creating an xml file (an actual file) which it writes to disk. returning the name of that file.

What you need to do is: build a string of all the XML you want to send to the client, echo that string

So:

Delete:

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

And replace it with

echo $XMLString;

This problem likely has nothing to do with whether or not you use jquery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM