File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes XMLHttpRequest messes up responseText Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "XMLHttpRequest messes up responseText" Watch "XMLHttpRequest messes up responseText" New topic

XMLHttpRequest messes up responseText

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429

I'm having a problem using the XMLHttpRequest to retrieve data from the server. When accessing the responseText of a certain resource, a character that I use for indenting is not recognized by either IE or Firefox and gets replaced with "?" (or in Firefox's case a ? on a black diamond). When the resource is accessed directly through the browser, it displays fine.

Here's where it gets more confusing:

The resource is actually a servlet which does an XSL transformation on a JDom XML document and returns the result. The character causing trouble is "&# 160;" in the stylesheet. But if I print out the entity directly in a JSP page, XMLHttpRequest returns it properly. The problem only happens if the entity is put into an JDom document through an XSL transformation, and then the string version of that document is returned through the XMLHttpRequest.

Again, if I access the resource directly, there are no "?"s.

I have to use "&# 160;" instead of "& nbsp;" because the latter is not recognized by the XSL transformer.

Thank you in advance for any help,
[ July 06, 2005: Message edited by: Yuriy Zilbergleyt ]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
Could it be the character encoding of the page are yo setting a doc type declaration?

Have you tried other escaped sequences and see if they appear right?

I am able to run a basic example and it is appearing correctly.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
I've tried "& amp;", and it appears correctly. Well actually I was trying to build "& nbsp;" by putting "& #38;nbsp;" in the stylesheet, but what came out was "&nbsp" in the HTML, and showed up in the browser as the literal characters " ".

I don't think its the encoding because I don't have a doctype (or anything similar) in either the XMLHttpRequest using page or the server-side include (<jsp:include> page, and the latter one works, while the former one displays ?'s.

And like I said, when the resource is a JSP page that that has nothing but

<% out.println("& #160; & #160"); %>

that works perfectly with XMLHttpRequest. But what I have is more along the lines of

Though it's a bit more involved and is inside a servlet instead of JSP page.

So somehow during the transformation, the "& #160;" in the stylesheet.xsl is changed into something else, just like "& #38;" is changed into "& amp;".
When I print out the result of the transformation on the server-side, the character is actually '�'.

But that seems to only cause a problem when the servlet output is retrieved using the XMLHttpRequest object. When the browser accesses the servlet directly by putting the it's URL mapping into the location bar, the indentation works fine, not a '?' in sight.

I really don't know what's going on.

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
All right, I solved the problem in my case by telling the JDom Format object I was using to escape all characters that could be escaped. Now everything indents properly even with XMLHttpRequest.

I'm still curious though why HTML returned through XMLHttpRequest's responseText would ever be different from the HTML taken directly from the resource.

I agree. Here's the link:
subject: XMLHttpRequest messes up responseText
It's not a secret anymore!