AJAX在IE中返回错误的字符集

时间:2022-12-20 18:31:37

What I'm trying to do

我想做什么

I am using $.get() on index.html page to get the contents of otherpage.html located on the same server and then display it in a DIV on index.html page. Both pages contain some accented characters (think Spanish, French and German) and are encoded with iso-8859-1.

我在index.html页面上使用$ .get()来获取位于同一服务器上的otherpage.html的内容,然后在index.html页面的DIV中显示它。两个页面都包含一些重音字符(想想西班牙语,法语和德语),并使用iso-8859-1进行编码。

Since both pages are generated with a 3rd party 'wysiwyg html authoring tool', I cannot change the encoding of the pages and I have no control over how servers are setup. I can only add my JS code.

由于这两个页面都是使用第三方'wysiwyg html创作工具'生成的,因此我无法更改页面的编码,也无法控制服务器的设置方式。我只能添加我的JS代码。

So what went wrong?

出了什么问题?

It all works perfect in Chrome, Firefox, Safari and Opera. However, IE10/9/8/7 returns garbled text. I get empty squares ` instead ofö,åandä`. I have searched all of * (ISO-8859-1 seems to cause a lot of headache) and my code includes most of those tips for enforcing encoding in headers etc. No success :(

它在Chrome,Firefox,Safari和Opera中都非常完美。但是,IE10 / 9/8/7会返回乱码文本。我得到空方格而不是öandand`。我搜索了所有的*(ISO-8859-1似乎引起了很多头痛),我的代码包括大多数用于在标题中强制执行编码的技巧。没有成功:(

Ok, show us your code!

好的,告诉我们您的代码!

I have setup test case on two servers: test one and test two. The code is below:

我在两台服务器上设置了测试用例:测试一个并测试两个。代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>the first page</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" charset="iso-8859-1">
function GetData() {

$.ajaxSetup({
    beforeSend: function(jqXHR) {
            jqXHR.overrideMimeType('text/html; charset=iso-8859-1')
            jqXHR.setRequestHeader('Accept','text/html; charset=iso-8859-1');
    },
    timeout:5000,
    contentType: 'text/html; charset=iso-8859-1',
    dataType: 'html',
    mimeType: 'text/html; charset=iso-8859-1',
    processData: false
})

$.post( 'otherpage.html' )
 .done( function(pagedata) { 
    console.log(pagedata)
    $('#result').html(pagedata);
 })
 .fail( function() { 
    $('#result').html('Request failed');
 })

}
</script>
</head>
<body>
<input type="submit" value="Get data" onclick="GetData()">
<div id="intro">Some characters: ö--å--ä.</div>
<div id="result">Click "Get data".</div>
</body>
</html>

otherpage.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>the other page</title>
</head>
<body>
<div>Hälsyt högg lått Xöööööööööz Zååååååååååååy Kääääääääääännn </div>
</body>
</html>

So your question is...?

所以你的问题是......?

What should I do in Javascript to make sure the text display correctly in IE10/9/8/7? I appreciate any constructive ideas.

我应该在Javascript中做些什么来确保文本在IE10 / 9/8/7中正确显示?我感谢任何建设性的想法。

1 个解决方案

#1


0  

IE screws up ISO-8859-1 encoding. Try using ISO-8859-15 encoding which is almost like ISO-8859-1. If that doesn't help, try setting document.charset="ISO-8859-1" in onsubmit handler of your callback for IE (for other browsers, <form accept-charset="ISO-8859-1"> should do the trick

IE搞砸了ISO-8859-1编码。尝试使用几乎像ISO-8859-1的ISO-8859-15编码。如果这没有用,请尝试在IE回调的onsubmit处理程序中设置document.charset =“ISO-8859-1”(对于其他浏览器,

应该执行招

P.S. I haven't yet run the code provided.

附:我还没有运行提供的代码。

#1


0  

IE screws up ISO-8859-1 encoding. Try using ISO-8859-15 encoding which is almost like ISO-8859-1. If that doesn't help, try setting document.charset="ISO-8859-1" in onsubmit handler of your callback for IE (for other browsers, <form accept-charset="ISO-8859-1"> should do the trick

IE搞砸了ISO-8859-1编码。尝试使用几乎像ISO-8859-1的ISO-8859-15编码。如果这没有用,请尝试在IE回调的onsubmit处理程序中设置document.charset =“ISO-8859-1”(对于其他浏览器,

应该执行招

P.S. I haven't yet run the code provided.

附:我还没有运行提供的代码。