AJAX and HTML encoding problem

Posted on 2010-09-07
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.
Question by:LeGrandi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33619983
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:

Author Comment

ID: 33627047
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?
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33628321
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 33628387
It still doesn't work
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33628479
Does the encoding on the original page and the AJAX page match?

Author Comment

ID: 33628843
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
LVL 83

Accepted Solution

Dave Baldwin earned 250 total points
ID: 33629495
Ok, the only way I was able to get the same encoding on the original page and the AJAX page is to force UTF-8 encoding.  Apparently javascript uses Unicode/UTF-8.  Trying to pass the Latin-1 characters thru it screws up the encoding and display.

Though some of my editors will read UTF-8, it was a real pain to get them to generate it.  Whatever they say, " á , é, í, " is not the same in Latin-1 and UTF-8.  Remove the <head></head> section from your AJAX text and find a way to force it to UTF-8 and use the meta header I gave you above.  Like I said, that's the only way I could find to make it work.

Author Comment

ID: 33660879

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


Author Closing Comment

ID: 33660883
Not total solution.

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Obsolete font tag need to convert to CSS 3 54
Jquery HTML() with ajax... replacing content 2 52
Send parameters via datatable ajax call 4 43
Diff of the day 2 41
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question