简体   繁体   English

IE的overrideMimeType替代方案

[英]overrideMimeType alternative for IE

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. 它适用于firefox,chrome。 My responseText will return char like Réunion, which will appear as weird symbols. 我的responseText将返回像Réunion这样的字符,它将显示为奇怪的符号。

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. 在加载AJAX的页面中,请确保将其放在顶部。

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");

You don't need anything fancy like overrideMimeType. 你不需要像overrideMimeType那样的任何东西。 Just make sure the encodings of your main and AJAX-loaded page are correct (preferrably UTF-8 for both). 只需确保主和AJAX加载页面的编码是正确的(两者最好是UTF-8)。 Most likely, you have forgotten the meta tag declaring the encoding. 最有可能的是,您忘记了声明编码的元标记

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. 在IE中,我访问responseBody以使用正确的字符创建responseText。 Here is my answer : 这是我的答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM