Javascript - XMLHttpRequest,结果始终未定义
我正在尝试测试 xml 文件是否具有标签“
”
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", "xmlfile.xml", false);
xmlhttp.send(null);
xml = xmlhttp.responseXML.documentElement;
var thegroup = xml.getElementsByTagName('group')[0];
if (!group) {
alert('No <group> in the XML: ' + xml);
return;
} else {
alert(xml + 'have a <group> tag');
}
即使我的 xml 文件具有标签“
”,结果是始终为负,并且变量“thegroup
”未定义。
“xml
”给我“[object Element]”
我的错误在哪里?
PS:我只对webkit感兴趣,暂时不关心IE、Opera或Firefox。
编辑:这是我的实际代码
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8">
<title>xmltest</title>
<script type="text/javascript">
function init() {
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", "xmlfile.xml");
xmlhttp.send(null);
xmlhttp.onreadystatechange = callbackFunction;
function callbackFunction(){
if (xmlhttp.readyState == 4){
xml = xmlhttp.responseXML.documentElement;
var group = xml.getElementsByTagName('group')[0];
console.debug(xml)
if (!group) {
alert('No <group> in the XML: ' + xml);
return;
} else {
alert(xml + 'have a <group> tag');
}
}
}
};
</script>
</head>
<body onLoad="init();">
</body>
</html>
和我的 xmlfile.xml:
<?xml version="1.0" ?>
<group type="vertical">
<name>name</name>
<title>title</title>
</group>
此时触发警报: XML 中没有
:[object Element]
所以也许我的问题只是在我尝试查找
标记的方式上?
I'm trying to test if the xml file have the tag "<group>
"
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", "xmlfile.xml", false);
xmlhttp.send(null);
xml = xmlhttp.responseXML.documentElement;
var thegroup = xml.getElementsByTagName('group')[0];
if (!group) {
alert('No <group> in the XML: ' + xml);
return;
} else {
alert(xml + 'have a <group> tag');
}
Even if my xml file have the tag "<group>
" the result is always negative, and the variable "thegroup
" is undefined.
"xml
" give me "[object Element]"
Where is my mistake?
PS: I'm only interested in webkit, I don't care about IE, Opera or Firefox for now.
EDIT : HERE IS MY ACTUAL CODE
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8">
<title>xmltest</title>
<script type="text/javascript">
function init() {
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", "xmlfile.xml");
xmlhttp.send(null);
xmlhttp.onreadystatechange = callbackFunction;
function callbackFunction(){
if (xmlhttp.readyState == 4){
xml = xmlhttp.responseXML.documentElement;
var group = xml.getElementsByTagName('group')[0];
console.debug(xml)
if (!group) {
alert('No <group> in the XML: ' + xml);
return;
} else {
alert(xml + 'have a <group> tag');
}
}
}
};
</script>
</head>
<body onLoad="init();">
</body>
</html>
and my xmlfile.xml :
<?xml version="1.0" ?>
<group type="vertical">
<name>name</name>
<title>title</title>
</group>
At this point the alert is triggered saying :
No <group>
in the XML: [object Element]
So maybe my problem is just on the way I try to find the <group>
tag ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XMLHttpRequest 是异步的,它不是这样工作的。当您使用 xmlhttp.send(null); 时,您必须定义当服务器响应数据时将执行的回调函数,否则您将尝试访问空数据。
代码看起来像这样:
这样,您就可以使用
onReadyStateChange
告诉浏览器在服务器每次发回响应时运行callbackFunction
。它测试readyState
是否为 4,这意味着请求已完全得到满足。XMLHttpRequest is asynchronous, it doesn't work that way. When you use
xmlhttp.send(null);
you have to define callback function that will be executed when the server responds with the data, otherwise you are trying to access empty data.The code would look something like this:
this way, you are using
onReadyStateChange
to tell the browser to runcallbackFunction
everytime the server sends back a response. It tests for thereadyState
to be 4 which means that the request has been completely served.什么是
组
?您指的是thegroup
吗?What is
group
? Did you meanthegroup
?