Link to home
Start Free TrialLog in
Avatar of jlawrence1
jlawrence1

asked on

AJAX xmlHttp.responseText encoding error

To whom it may concern:

I am using AJAX to retrieve data from the server. The whole process works as expected except for one problem. at this line.

 document.getElementById("ProductList").innerHTML=xmlHttp.responseText;;

The returned data displays question marks when trying to display é or any other value above ASCII 128. The values at the server are correct. The caller web site has no problems displaying the appropriate characters (charset=iso-8859-1). Two weeks and no solution.

MTIA
Jim
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

First, have you tried to test just the server part of the AJAX script.  In other words if the ajax script calls ajax.asp then try to bring up that page in your browser.  What is the result?  Of course it should be a partial page but do the characters appear OK?

What server language is used?

A common fix for something like this is to have the server script include the encoding type in the start of the script.  Most languages have a way to set this "header" and it can often fix the problem.  Let me know the server language and I can probably provide details.  If this is the problem then the test above should give the same ? character instead of the valid one.

Let me know if you have a question or need more information.  If this doesn't work then I can suggest one or two other "fixes" I have used before.

bol
If the language is PHP then you might try adding the line below to the start of the server script run by the AJAX code.

header("Content-type: text/html; charset=iso-8859-1");

If you do use PHP then let me know how that works.  If you use another language then just let me know which.  The key is to make sure the AJAX response object gets the information with the right encoding.

bol
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stanscott2
stanscott2

b0lsc0tt has the right idea, but you have to specify your character set in the Ajax request:

 xmlHTTP.setRequestHeader("Content-Type","text/html; charset:ISO-8859-1");

where xmlHTTP is the object reference to the xmlHTTPRequest object.
Avatar of jlawrence1

ASKER

The server language is ASP and the server is MS SQL 2005

The JavaScript code that make the cali is, if that would help:
var url="http://online.creativesystemdesigns.com/projects/programming3.asp";
                  url=url+"?pcode="+pcode;
                  url=url+"&position="+position;

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

The ASP Server page has the follow meta tag heading:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
...same as the caller page.

HTH
Jim
I'm glad you tried the suggestion in that comment.  If you really do have a meta tag, which puzzles me in an AJAX response, the tag will not help since this is part of a page, not a new page.  The header from the server is still relevant and so that is why the ASP script worked.

I'm glad I was able to help.  Thanks for the grade, the points and a fun question.

@Stanscott2 - maybe it is too late (it has been a long day) but isn't setRequestHeader going the wrong way?  In other words it would be used for information you were sending from the object.  It would not effect something in the response (from the server to the browser [and XMLHttp object]).  At this time there is no way I know for the XMLHttp object to "change" the content type from the server.  I hope this clarifies why I suggested what I did.  Let me know if I misunderstood something you said or if you have a question about what I said.

bol