带有希伯来语字符的XMLHttpRequest



XMLHttpRequest中的文本包含希伯来语字符时,我的代码似乎会出错,所以我做了一个小例子来演示这个问题。

小代码示例加载页面,然后用从服务器获得的内容替换div的内容。既然它调用相同的网页,那么它应该用相同的内容替换它。

当div中的按钮包含英文字母时,代码运行良好,但如果字母超过ASCII 127,则会拧紧按钮。

我做了一些测试,发现如果我将文件保存为Unicode,那么它就可以正常工作。。。问题是我无法将此文件保存为Unicode,因为它是:

  1. 将文件大小增加一倍
  2. 在html文件中写入一些数据的硬件,用ASCII编写

这个小例子只是加载自己:

<HTML><HEAD><TITLE>Test</TITLE>
</HEAD>
<BODY onload="readPage()">
<Div id='Hello'>
<Button>שלום</Button>
</Div>
<Script>
var xmlHttp;
function readPage()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="/xampp/2.html?dummy=" + new Date().getTime();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url, true);
xmlHttp.setRequestHeader("Content-Type", "text/plain;charset=ISO-8859-1");
xmlHttp.setRequestHeader("Accept-Charset", "ISO-8859-1");
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
var x = ""; 
//alert(xmlHttp.responseXML);
//alert(xmlHttp.responseText);
if (xmlHttp.responseText.match(/<Div id='Hello'>([sS]*?)</Div>/i))
{
x = xmlHttp.responseText.match(/<Div id='Hello'>([sS]*?)</Div>/i)[1];
//x = x.replace(/"/g,'');

var pdata= document.all ? document.all["Hello"] :
document.getElementById("Hello");
alert(pdata.innerHTML);
alert(x);
pdata.innerHTML=x;
alert(pdata.innerHTML);
}
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</Script>
</BODY></HTML>

我尝试更改字符集和内容类型,但不起作用。

我做错了什么?

(顺便说一句,我不能使用任何jQuery的东西)。

谢谢。

您可以尝试手动对字母进行编码。这是一个非常缓慢和糟糕的解决方案,但它可能会对你有所帮助q包含希伯来语数据(在javascript上)

q=q.replace(/א/gi,"%E0");
q=q.replace(/ב/gi,"%E1");
q=q.replace(/ג/gi,"%E2");
q=q.replace(/ד/gi,"%E3");
q=q.replace(/ה/gi,"%E4");
q=q.replace(/ו/gi,"%E5");
q=q.replace(/ז/gi,"%E6");
q=q.replace(/ח/gi,"%E7");
q=q.replace(/ט/gi,"%E8");
q=q.replace(/י/gi,"%E9");
q=q.replace(/כ/gi,"%EB");
q=q.replace(/ך/gi,"%EA");
q=q.replace(/ל/gi,"%EC");
q=q.replace(/מ/gi,"%EE");
q=q.replace(/ם/gi,"%ED");
q=q.replace(/נ/gi,"%F0");
q=q.replace(/ן/gi,"%EF");
q=q.replace(/ס/gi,"%F1");
q=q.replace(/ע/gi,"%F2");
q=q.replace(/פ/gi,"%F4");
q=q.replace(/ף/gi,"%F3");
q=q.replace(/צ/gi,"%F6");
q=q.replace(/ץ/gi,"%F5");
q=q.replace(/ק/gi,"%F7");
q=q.replace(/ר/gi,"%F8");
q=q.replace(/ש/gi,"%F9");
q=q.replace(/ת/gi,"%FA");

好的,我不确定XAMPP的web服务器为我使用的特定HTML发送了什么,但在特定的硬件上,我们编写了web服务器,它发送:

HTTP/1.1 200 OK
MIME-version: 1.0
Content-Type: text/html
Content-length: 11280
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0

一个解决方案是将网页保存为Unicode,这对我们不利。另一个似乎有效的选项是告诉浏览器我们想要一个特定的编码。在本例中为Windows-1255。

我所要做的就是更改从网站发送的标题,使其也包含字符集,现在它工作正常:

HTTP/1.1 200 OK
MIME-version: 1.0
Content-Type: text/html; charset=Windows-1255
Content-length: 11280
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0

最新更新