We help IT Professionals succeed at work.

AJAX and HTML encoding problem

LeGrandi
LeGrandi asked
on
601 Views
Last Modified: 2013-11-18
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.
Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
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

Author

Commented:
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 BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
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

Author

Commented:
It still doesn't work
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Does the encoding on the original page and the AJAX page match?

Author

Commented:
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
Fixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi,

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

Thanks

Author

Commented:
Not total solution.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.