Solved

AJAX and HTML encoding problem

Posted on 2010-09-07
9
570 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.
0
Comment
Question by:LeGrandi
  • 5
  • 4
9 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
0
 

Author Comment

by:LeGrandi
Comment Utility
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?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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

0
 

Author Comment

by:LeGrandi
Comment Utility
It still doesn't work
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Does the encoding on the original page and the AJAX page match?
0
 

Author Comment

by:LeGrandi
Comment Utility
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
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
Comment Utility
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.
0
 

Author Comment

by:LeGrandi
Comment Utility
Hi,

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

Thanks
0
 

Author Closing Comment

by:LeGrandi
Comment Utility
Not total solution.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HTML table 7 54
Bootstrap Datable Spinner 3 27
Element alignment and word wrapping 9 23
Performance  Html.BeginForm vs jQuery 3 10
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now