Javascript - XMLHttpRequest,结果总是未定义的 [英] Javascript - XMLHttpRequest, result are always undefined

查看:134
本文介绍了Javascript - XMLHttpRequest,结果总是未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试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(XML中的'No< group>:+ + xml);
返回;
} else {
alert(xml +'有一个< group>标签');
}

即使我的xml文件标签为< ; group> 结果始终为负数,变量 thegroup 未定义。



xml 给我[对象元素]



我的错误在哪里?



PS:我只对webkit感兴趣,我现在不关心IE,Opera或Firefox。



<编辑:这是我的实际代码

 <!DOCTYPE HTML PUBLIC -  // IETF // DTD HTML // EN> 
< html>
< head>
< meta http-equiv =CONTENT-TYPEcontent =text / html; charset = UTF-8>
< title> xmltest< / title>
< script type =text / javascript>

函数init(){

var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open(GET,xmlfile.xml);
xmlhttp.send(null);
xmlhttp.onreadystatechange = callbackFunction;

函数callbackFunction(){
if(xmlhttp.readyState == 4){
xml = xmlhttp.responseXML.documentElement;
var group = xml.getElementsByTagName('group')[0];
console.debug(xml)
if(!group){
alert(XML中的'No< group>:'+ xml);
返回;
} else {
alert(xml +'有一个< group>标签');
}
}
}

};

< / script>
< / head>
< body onLoad =init();>

< / body>
< / html>

和我的xmlfile.xml:

 <?xml version =1.0?> 

< group type =vertical>
< name> name< / name>
< title> title< / title>
< / group>

此时触发警报说:
< XML中的; group> :[object Element]



所以也许我的问题就在于我试图找到<$ c的方式$ c>< group> tag?

解决方案

XMLHttpRequest是异步的,它不起作用那样。当您使用 xmlhttp.send(null); 时,您必须定义将在服务器响应数据时执行的回调函数,否则您将尝试访问空数据。
代码看起来像这样:

  var xmlhttp = new window.XMLHttpRequest(); 
xmlhttp.open(GET,xmlfile.xml);
xmlhttp.send(null);
xmlhttp.onreadystatechange = callbackFunction;



函数callbackFunction(){
if(xmlhttp.readyState == 4){
xml = xmlhttp.responseXML.documentElement;
var thegroup = xml.getElementsByTagName('group')[0];
if(!group){
alert(XML中的'No< group>:+ + xml);
返回;
} else {
alert(xml +'有一个< group>标签');
}
}
}

这样,你正在使用每次服务器发回响应时, onReadyStateChange 告诉浏览器运行 callbackFunction 。它测试 readyState 为4,这意味着请求已完全提供。


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 ?

解决方案

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.

这篇关于Javascript - XMLHttpRequest,结果总是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆