3

This is a question that never has a proper answer, i have search the net many times and i couldnt find a solution.

xhr.open("GET", fullurl, true);
if(xhr.overrideMimeType) xhr.overrideMimeType("text/html; charset=ISO-8859-1");
xhr.send(null);

xhr.onreadystatechange  = function(){ 
if(xhr.readyState  == 4){
if(xhr.status  == 200) 
alert(xhr.responseText);

It works for firefox, chrome. My responseText will return char like Réunion, which will appear as weird symbols.

I tried many methods like encoding and decoding, setting header in response file which does not work. i'm out of ideas. Please help somebody.


**SOLUTION**

In your main file, make sure you set your content type and charset.

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

In your AJAX-loaded page, make sure you have this at the top.

header('Content-Type: text/html; charset=iso-8859-1'); 

Problem solved, your a happy man. this is no longer needed.

if(xhr.overrideMimeType) xhr.overrideMimeType("text/html; charset=ISO-8859-1");
john
  • 35
  • 1
  • 4

2 Answers2

1

You don't need anything fancy like overrideMimeType. Just make sure the encodings of your main and AJAX-loaded page are correct (preferrably UTF-8 for both). Most likely, you have forgotten the meta tag declaring the encoding.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • Use HTTP headers, they have high precedence then meta equivs, and don't restart parsing if the browser realizes it was using the wrong encoding. – Quentin Oct 03 '10 at 17:39
  • are you asking me to set at both my main and ajax-loaded page? if yes, after setting it, my main page also having same problem, and the ajax result is still the same. please advise – john Oct 03 '10 at 18:05
  • i'm sorry but that fixed the problem but my charset needed would be iso-8859-1. Thankyou very much – john Oct 03 '10 at 19:31
0

I can't change the encoding with the meta tag and access to the server. In IE I access the responseBody to create a responseText with the correct chars. Here is my answer :

https://stackoverflow.com/a/22030309/3353928

Community
  • 1
  • 1
George G
  • 11
  • 2