View Single Post
Old 07-01-2008, 10:03 AM   PM User | #4
hotwheelharry
Regular Coder

 
Join Date: Jun 2008
Posts: 102
Thanks: 6
Thanked 9 Times in 9 Posts
hotwheelharry is an unknown quantity at this point
the ajax works fine, it's just when...

document.getElementById("div").innerHTML = xmlReq.responseText;

creates an error.

if you do innerText, it works fine too, just innerHTML is the problem. Yes it (the div) is hard-coded.

What could be the only reasons innerHTML being set to responseText causes an error. This is only in IE.

Everything is rendered. It is just the ajax code, nothing else on the page.
If you really want to see it...
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>


URL: <input type="text" id="url" /> <input type="button" value="Go" onclick="callback(document.getElementById('url').value)" />

<br/>

<div id="ajaxUpdate">
</div>

<script>
function getAjax(){
    var request = false;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } 
    else if (window.ActiveXObject) {
        try {
            request = new ActiveXObject("MSXML2.XMLHTTP");
        } 
        catch (e1) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e2) {
                //Failed to get XMLHttpRequest.
            }
        }
    }
    
    return request;
}  

var request;
var begin;
var time;
var response = "";
function callback(url){
    request = getAjax();
    request.open("GET", url, true); // true async
    request.onreadystatechange = handler;
        begin = (new Date()).getTime();
    request.send(null);
}
function handler(){
    var panel = document.getElementById("ajaxUpdate");
    
    switch(request.readyState){
    case 0:
        panel.innerHTML = "Uninitialized!";
        break;
    case 1:
        panel.innerHTML = "Open";
        break;
    case 2:
        panel.innerHTML = "Sent";
        break;
    case 3:
        panel.innerHTML = "Receiving";
        break;
    case 4:
        time = ( (new Date()).getTime() - begin) / 1000;
        panel.innerHTML = "Status: " + request.status + " (" + request.statusText + ")";
        panel.innerHTML += "<br/>Time: " + time.toFixed(3) + " (sec)";
        panel.innerHTML += "<br/>Response Headers: <br/>" + request.getAllResponseHeaders();
        
        panel.innerHTML += "<br/><br/>" + request.responseText;
        request = null;
	break;
    }
}
</script>

</body>
</html>
If I run it locally (C:\file.html) versus on VS localhost, it works fine. Then if I put it on the localhost and do ajax for something like "localhost/website/text.txt" I get the original error at "innerHTML = responseText" line. If I go to same location in a browser, it works fine, I get the text file. xmlReq.responseText just won't work with localhost.

So I guess this was a bit different problem than I originally was implying. Sorry.

Last edited by hotwheelharry; 07-01-2008 at 10:09 AM..
hotwheelharry is offline   Reply With Quote