Javascript xml中的阿拉伯语

Javascript xml中的阿拉伯语,javascript,html,xml,Javascript,Html,Xml,我有以下代码: html代码: <script> function loadXMLDoc(filename) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",filename,false); xhttp.send(); return xhttp.responseXML;

我有以下代码:
html代码:

<script>
function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
var xmlDoc = loadXMLDoc('users.xml');
var x = xmlDoc.getElementsByTagName('usr');
document.write(x.length);
</script>
<?xml version="1.0" encoding="utf-8"?>
<users xmlns="http://localhost" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost users.xsd">
<usr><age>25</age><country>الاردن</country></usr>
<usr><age>30</age><country>مصر</country></usr>
</users>
<!DOCTYPE html unicode="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
include 'users.html';
?>
</body>
</html>

函数loadXMLDoc(文件名)
{
if(window.XMLHttpRequest)
{
xhttp=newXMLHttpRequest();
}
其他的
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”,filename,false);
xhttp.send();
返回xhttp.responseXML;
}
var xmlDoc=loadXMLDoc('users.xml');
var x=xmlDoc.getElementsByTagName('usr');
文件。书写(x.长度);
xml代码:

<script>
function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
var xmlDoc = loadXMLDoc('users.xml');
var x = xmlDoc.getElementsByTagName('usr');
document.write(x.length);
</script>
<?xml version="1.0" encoding="utf-8"?>
<users xmlns="http://localhost" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost users.xsd">
<usr><age>25</age><country>الاردن</country></usr>
<usr><age>30</age><country>مصر</country></usr>
</users>
<!DOCTYPE html unicode="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
include 'users.html';
?>
</body>
</html>

25الاردن
30مصر
我想在另一个html文档中包含html代码,如下所示:
html代码:

<script>
function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
var xmlDoc = loadXMLDoc('users.xml');
var x = xmlDoc.getElementsByTagName('usr');
document.write(x.length);
</script>
<?xml version="1.0" encoding="utf-8"?>
<users xmlns="http://localhost" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost users.xsd">
<usr><age>25</age><country>الاردن</country></usr>
<usr><age>30</age><country>مصر</country></usr>
</users>
<!DOCTYPE html unicode="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
include 'users.html';
?>
</body>
</html>

javascript代码的输出是:0,而它应该是2,如果我将xml文档中的阿拉伯语文本替换为英语文本,则输出将变为2。

为什么阿拉伯文会破坏代码?

我不是舒尔,如果您使用ajax,为什么要在php页面中包含问题中未描述的HTML('users.HTML')。无论如何,我在你的代码中看到了一些小问题

现代浏览器中utf8的正确格式如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8"><!-- how to set utf8 in modern browsers -->
<title>Users</title>
<script>
function show(){
 document.body.innerHTML=this.responseXML.getElementsByTagName('usr').length
}
window.onload=function(){
 with(new XMLHttpRequest)open('get','users.xml'),onload=show,send()
}
</script>
</head>
<body>
</body>
</html>

但是有很多方法可以将编码设置为utf8&包含文件

就个人而言,如果您使用ajax,我不会使用您的方法。。。如果您坚持发布您的
users.html
,以便我们了解导致问题的更多信息

js代码正确返回长度2,ajax是异步的,应该是异步的。 关于ajax的更多信息:

我将
一起使用的原因如下:

注意:在另一个html页面中包含整个html页面不是正确的方法,您应该只包含必要的部分(html元素)


如果您对我的代码有任何疑问,请提问。

无法复制。您是否可以显示相关的HTTP头和/或提供用于测试的实时URL?浏览器控制台中是否有任何错误消息?@JukkaK.Korpela这是我在运行var_dump(headers_list())时得到的消息;“array(2){[0]=>string(24)”X-Powered-By:PHP/5.3.24“[1]=>string(23)“Content-type:text/html”}”和否我没有实时url,浏览器控制台中没有错误消息。如果将
脚本
元素直接插入html代码而不是使用PHP进行包含,会发生什么?(我只是想把问题简化成一个简单的例子。)@JukkaK.Korpela同样的问题。我的测试,代码与问题中的一样,但直接包含HTML代码,而不是PHP:在IE、Chrome、Firefox上工作。