views:

394

answers:

3

Hi there, folks.

I've already asked here a question about this issue. The thing is that I would really appreciate to get your help on this, as I can't seem to detect and solve the problem I'm having with an AJAX functionality on a site that I'm currently developing.

I have a webform that makes an asynchronous call to a handler (.ashx) that delivers a XML response that is later processed by a Javascript client-side function that places it's contents into the user-interface.

I'm attaching an example of the response generated by my handler, and what I would like to know is why does Internet Explorer gets a "null" reference when I parse the XML response (see above). I already have it working on Firefox, Chrome and Opera and it works. It even works on Internet Explorer, when the XML response element does not contain HTML content. Weird? Can anyone help me out with this?

Code snipped of my Javascript object, to make assynchronous AJAX calls to that handler:

if (window.XMLHttpRequest) {
    // Mozilla/Safari/IE 7+
    http = new window.XMLHttpRequest();
    if (http.overrideMimeType)
    http.overrideMimeType("text/xml");
}
else {
    // IE 5/6
    http = new ActiveXObject("Microsoft.XMLHTTP");
}

XML Response returned by the handler (checked via Firebug using Firefox):

<reply>
    <message>
        <messageId>2</messageId>
        <body><![CDATA[I'm sending you this message to invite you to join us!<br/><a href="http://www.whitehouse.gov"&gt;White House.gov</a><br/>Thank you for your time.]]></body>
    </message>
</reply>

Client-side Javascript function to affect the user-interface with the data returned via AJAX (in this example I've simplified it by simply trying to print out the message id):

function GetMessageContentsCallback(args, resp) {

    // Response only contains one "message" element (at the 1st position)
    var message = resp.getElementsByTagName('message')[0];

    // This alert prints "null" on IE, but works OK on Firefox, Chrome & Opera!
    alert("message = " + message );

    /* ...and IE then breaks here, with this message:
       "Microsoft JScript: 'null' is null or not an object" */
    var messageId = message.getElementsByTagName('messageId')[0].firstChild.nodeValue;
    alert("messageId = " + messageId);
}

What the hell am I doing wrong here? When this XML reply does not contain HTML content, it works. When it contains HTML content, Internet Explorer doesn't work with my solution... although everybody else (Firefox, Chrome and even Opera) does! Damn! :(

+1  A: 

The best way is to translate all invalid xml characters to its equivalent entities or placed the that value with possible invalid xml characters with CDATA from the server side.

You should encode the xml values on your ASHX before it gets to the client:

Instead of having this xml:

<reply>
    <message>
        <messageId>2</messageId>
        <body>I'm sending you this message to invite you to join us!<br/><a href="http://www.whitehouse.gov"&gt;White House.gov</a><br/>Thank you for your time.</body>
    </message>
</reply>

New XML format should be:

<reply>
    <message>
        <messageId>2</messageId>
        <body>I'm sending you this message to invite you to join us!&lt;br/&gt;&lt;a href=&quot;http://www.whitehouse.gov&amp;quot;&amp;gt;White House.gov&lt;/a&gt;&lt;br/&gt;Thank you for your time.]]></body>
    </message>
</reply>

or the better one:

<reply>
    <message>
        <messageId>2</messageId>
        <body><![CDATA[I'm sending you this message to invite you to join us!<br/><a href="http://www.whitehouse.gov"&gt;White House.gov</a><br/>Thank you for your time.]]></body>
    </message>
</reply>
jerjer
I had forgotten to put them in the text, but I'm already sending my handler XML response wrapped with "<![CDATA[" and "]]>".
XpiritO
tried changing to "resp.getElementsByTagName('message').documentElement", but doesn't work either...
XpiritO
can you check resp.parseError.errorCode if it is not 0 then, there is parse error occured, if not 0 try alert(resp.parseError.reason); //.srcText
jerjer
I have tested the sample xml with CDATA, it is working from my end (I am using IE8)
jerjer
Thanks for your reply. In fact, I get an error code and the error message says I have invalid characters. But why does it only occur on IE? Damn!
XpiritO
If in case this should not worked for you, you might as well try changing your data format into JSON,JSON shouldn't be problematic as XML in terms of browser compatibility.
jerjer
one more thing, please check if the content-type of the response from your ASHX is text/xml or application/xml, you might need Fiddler as well to check for the response.
jerjer
My handler is returning ContentType="text/xml". I cannot have JSON as I want to have HTML content on my response, isn't it?
XpiritO
@XpiritO: You have encoding issue i beleive, you can check my edited post.
Emrah GOZCU
A: 

New

Ok, as far as i read your responses you gave, i start to think you have some special charachter in your xml. I would recommend that you to add xml declarition on top of the requested xml response like this <?xml version="1.0" encoding="ISO-8859-1"?> but appropriate encoding you have in the xml response.

Here is the XML Encoding details. Here is another approach to force IE get correct encoding.

Previous Post

Do you check XmlHttpRequest status in your callback function, like this below?

if(http.readyState == 4)
   if(http.status == 200)
      var res = http.responseXML;

Here is some info about onreadystatechange.

Emrah GOZCU
Yes, I do that in my callback.
XpiritO
Thanks for your reply. I hadded "encoding='ISO-8859-1'" attribute to my XML declaration, but it didn't solve the problem. Neither solved that "xhr.overrideMimeType('text/html; charset=ISO-8859-1');", as this function is not supported on Internet Explorer.
XpiritO
@XpiritO: Did you try UTF-8?
Emrah GOZCU
I cannot use "UTF-8" as my response contains some "ISO-8859-1" encoded characters like "à", "ç", "ã" and so...
XpiritO
A: 

No one ever had this problem? No one can help me out with this issue? :(

XpiritO