Avatar of LeGrandi
LeGrandi
Flag for Spain asked on

AJAX and HTML encoding problem

I have a problem with encoding with AJAX.

I am not using a framework, just standard javascript. Through AJAX I load a HTML file and display the responseText inside a div (innerHTML = req.responseText).

The problem is that it does not load a client-server request, but a regular HTML file, so the encoding goes crazy and things like á , é, í, etcetera are not displayed correctly. I would like to know if there is some way to indicate the XHMTLRequest object to load the response in a given encoding, or to give the HTML file that is loaded an specific header for the Ajax request to understand it correclty.
Web ComponentsAJAXHTML

Avatar of undefined
Last Comment
LeGrandi

8/22/2022 - Mon
Dave Baldwin

It should display using the character set of the page that is loading it.  Is it the same as the page you are loading thru AJAX?  These pages are the most complete description of using AJAX that I have found: http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml
LeGrandi

ASKER
Actually it works that way in Chrome, but not in Firefox or IE. How should I indicate in the html page which is loaded the charset?
Dave Baldwin

Use this line in the header, preferably as the first line below <header>.
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
LeGrandi

ASKER
It still doesn't work
Dave Baldwin

Does the encoding on the original page and the AJAX page match?
LeGrandi

ASKER
The ajax, indeed, is not a Full page with <html></html> and <body></body>, but an HTML fragment and now has a <head></head> with the meta content-type inside
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
LeGrandi

ASKER
Hi,

it definitely doesn't work, so I will html encode the conflictive characters which is far simpler.

Thanks
LeGrandi

ASKER
Not total solution.