为什么非 ASCII 字符显示为奇怪的符号? [英] Why are non-ASCII characters displayed as weird symbols?
问题描述
我这里有两个案例:
我的数据库包含很多我想获取到页面的信息.其中一些信息是 name,其中包含非 ASCII 字符,例如 Uwe Rülke
- 旧的解决方案,效果很好:
我从数据库中获取数据并直接从 VB while
循环填充页面.在这种情况下,所有字符都正确显示Uwe Rülke
.
- 无法正常工作的新解决方案:
VB While
循环不会将数据直接抛出到页面,而是以 JavaScript 字符串的形式(通过不时不时调用数据库来提高性能).但是当我使用存储在 JavaScript 变量中的信息时,我得到了这样的信息:Uwe R lke
.
在这两种情况下,页面的编码是:
我哪里做错了?
这是用于获取(从数据库)然后保存到 JavaScript 字符串的代码.
我正在使用来自名为 ISEquery
的页面中的 AJAX LOAD
来构建特定请求并从数据库中查询它.它用于获取数据作为 Excel 文件或普通 HTML.在这一点上,人物表现得很好.
然后奇迹发生了,角色被误传了.我在 exctractFields
函数中检查了它:
$("").load("ISEquery.asp?isExcel=0&" + info, function(){//将字段提取到数组中var 行 = "";var 选项 = "";$(this).children().each(function(index){var fieldsArray = exctractFields($(this).html());行 += createISELine(fieldsArray);选项 += createISELine_ComboBox(fieldsArray);});});
我按照从服务器到页面的字符串,发现它在 AJAX LOAD
之后被错误表示,所以我找到了 charset="iso-8859-1"
才能使其工作,而不是 charset="UTF-8"
.
所以最后的答案是:
-在 HTML 页面中编码:
-对 Ajax 数据进行编码:
$.ajaxSetup({'beforeSend':函数(xhr){xhr.overrideMimeType('text/html; charset=iso-8859-1');},});
现在字符显示正确.
(主要来自 Aaron Digulla 的回答.)
I have two cases here:
My database contains a lot of information which I want to fetch to the page. Some of this information is name which contain non-ASCII characters like Uwe Rülke
- Old solution which works well:
I fetch the data from the database and populate the page directly from a VB while
loop. In this case all the chars are displaying correctly Uwe Rülke
.
- New solution which doesn't work properly:
The VB While
loop doesn't throw the data directly to the page, rather in a JavaScript strings (to enhance performance by not calling the database each now and then). But when I used the information stored in the JavaScript variables, I got something like this: Uwe R�lke
.
In both cases, the page's encoding is:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
Where did I go wrong?
This is the code used to fetch (from the database) and then save to JavaScript strings.
I'm using AJAX LOAD
from a page called ISEquery
to build a specific request and query it from the database. It is used to either fetch data as an Excel file or as plain HTML. At this point the characters are well represented.
Then the magic happens, and the characters get mis-represented. I checked it in the exctractFields
function:
$("<div></div>").load("ISEquery.asp?isExcel=0&" + info, function(){
// Extracting the fields into an array
var rows = "";
var options = "";
$(this).children().each(function(index){
var fieldsArray = exctractFields($(this).html());
rows += createISELine(fieldsArray);
options += createISELine_ComboBox(fieldsArray);
});
});
I followed the string from server to the page and found that it is gets misrepresented after the AJAX LOAD
, so I found this answer which resolved my problem. Although I had to use the charset="iso-8859-1"
for it to work rather than charset="UTF-8"
.
So the final answer is:
-Encoding in the HTML page:
<meta http-equiv="Content-Type" content="text/html"; charset="iso-8859-1">
-Encoding the Ajax data:
$.ajaxSetup({
'beforeSend' : function(xhr) {
xhr.overrideMimeType('text/html; charset=iso-8859-1');
},
});
And now characters are displayed correctly.
(The lead was from Aaron Digulla's answer.)
这篇关于为什么非 ASCII 字符显示为奇怪的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!