Javascript - XMLHttpRequest,结果始终未定义

发布于 2025-01-02 06:52:04 字数 1823 浏览 1 评论 0原文

我正在尝试测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2025-01-09 06:52:04

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');
    }
  }
}

这样,您就可以使用 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:

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.

oО清风挽发oО 2025-01-09 06:52:04
var thegroup = xml.getElementsByTagName('group')[0];
如果(!组){
    alert('XML 中没有 : ' + xml);
    返回;
} 别的 {
    Alert(xml + '有一个  标签');
}

什么是?您指的是thegroup吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文