Javascript - XMLHttpRequest,结果总是未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9151834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - XMLHttpRequest, result are always undefined
提问by Julian
I'm trying to test if the xml file have the tag "<group>
"
我正在尝试测试 xml 文件是否有标签“ <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 文件有标签“ <group>
”,结果总是负数,并且变量“ thegroup
”是未定义的。
"xml
" give me "[object Element]"
“ xml
”给我“[对象元素]”
Where is my mistake?
我的错误在哪里?
PS: I'm only interested in webkit, I don't care about IE, Opera or Firefox for now.
PS:我只对 webkit 感兴趣,暂时不关心 IE、Opera 或 Firefox。
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 :
和我的 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]
此时触发警报说:<group>
XML 中的否:[对象元素]
So maybe my problem is just on the way I try to find the <group>
tag ?
所以也许我的问题只是在我试图找到<group>
标签的路上?
回答by André Al?ada Padez
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:
XMLHttpRequest 是异步的,它不能那样工作。使用xmlhttp.send(null);
时必须定义回调函数,当服务器响应数据时将执行该回调函数,否则您将尝试访问空数据。代码看起来像这样:
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 thegroup = xml.getElementsByTagName('group')[0];
if (!group) {
alert('No <group> in the XML: ' + xml);
return;
} else {
alert(xml + 'have a <group> tag');
}
}
}
this way, you are using onReadyStateChange
to tell the browser to run callbackFunction
everytime the server sends back a response. It tests for the readyState
to be 4 which means that the request has been completely served.
这样,您就onReadyStateChange
可以在callbackFunction
每次服务器发回响应时告诉浏览器运行。它测试为readyState
4,这意味着请求已被完全服务。
回答by Lightness Races in Orbit
var thegroup = xml.getElementsByTagName('group')[0]; if (!group) { alert('No <group> in the XML: ' + xml); return; } else { alert(xml + 'have a <group> tag'); }
var thegroup = xml.getElementsByTagName('group')[0]; if (!group) { alert('No <group> in the XML: ' + xml); return; } else { alert(xml + 'have a <group> tag'); }
What is group
? Did you mean thegroup
?
什么是group
?你的意思是thegroup
?