Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2008, 02:35 AM   PM User | #1
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
Why is IE dumb (innerHTML = xmlReq.responseText generates error)

if I have code like

Code:
function handler(){
    if (xmlReq.readyState == 4){
        document.getElementById("DivEl").innerHTML = xmlReq.responseText;
    }
}
where handler is the handler for xmlReq.onreadystatechange.

Why does IE generate an error ("Unknown runtime error") at the line where responseText is assigned to innerHTML???

Firefox works fine.

I thought maybe it was because there was already a <doctype> in the page and also in the response page. But even after taking them both out, no change.

It doesn't work in IE6, and I don't have 7 cause it's on a different computer now, so I haven't tried on 7.

Strangely enough it allows "innerText = responseText;" to work.
Could this be one of IE's lame attempts to be secure? idk.

Also if you alert(responseText), it alerts the text that is supposed to respond! Why can't it just put the text in the stupid box! I don't see what is preventing the text (that is there) from going in the stupid div!

Please excuse my madness, IE bugs me.

Please tell me why IE does not allow this, and maybe a solution to it.

Thanks,
Harry

Last edited by hotwheelharry; 06-26-2008 at 02:45 AM..
hotwheelharry is offline   Reply With Quote
Old 06-27-2008, 09:44 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Without seeing all of the code or how you are calling this. Is the page fully rendered / onload has already fired? Is this div you are putting the text into hardcoded n the page or is it dynamic.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 06-27-2008, 11:48 PM   PM User | #3
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
as stated, it's difficult without more code, but should your handler not be receiving xmlReq as it's argument?
__________________
My thoughts on some things: http://codemeetsmusic.com
And my scrapbook of cool things: http://gjones.tumblr.com
GJay is offline   Reply With Quote
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
Old 04-03-2009, 07:42 PM   PM User | #5
yorgo
New to the CF scene

 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
yorgo is an unknown quantity at this point
Angry Try this

i included this line in my php script before sending the response back to the browser.

header('Content-type: text/html; charset=UTF-8');

worked for me.

PS - I friggin hate IE... with a passion.
yorgo is offline   Reply With Quote
Old 04-03-2009, 10:55 PM   PM User | #6
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
You also shouldn't have your script in your <body> like that.
__________________
Feel free to e-mail me if I forget to respond ;)
ohsosexybrit@gmail.com
itsallkizza is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:01 AM.


Advertisement
Log in to turn off these ads.