[XML和Javascript]我无法获取XML数据. [英] [XML and Javascript] I can't get XML data.

查看:84
本文介绍了[XML和Javascript]我无法获取XML数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!全部
我很困惑用javascript获取XML数据.
我检查我的代码还可以,但找不到与Google相同的问题.
当我使用responseXML方法获取XML数据时,无法获取有效数据,并且它返回未定义"字符串.但是,如果我在获取XML之前添加警报消息,则可以获得正确的数据.为什么?我无法解释这种情况.
我的代码如下.

Hi! All
I feel confused that I get XML data with javascript.
I check my code that''s ok and I can''t find the same issue with google.
When I get the XML data using responseXML method, I can not get valid data and it returns ''undefined'' string. But, if I add alert message before I get XML, I can get correct data. Why? I can not explain the situation.
My code is as bellow.

<pre lang="xml"><! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Testing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function myObj(objip, objkey) {
    var ip = objip;
    var key = objkey;
    var data;
    var xmlhttp;
    this.DisplayInfo = Display;
    this.myxml = do_xml;
    function Display()
    {
        alert(data); // ''undefined'' ???? why ????
        alert(data); // ''success''
    }
    function parseXML()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            data = (xmlhttp.responseXML.getElementsByTagName(key).item(0).firstChild.data).toString();
        }
    }
    function do_xml()
    {
        var value;
        xmlhttp = createRequest();
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType(''text/xml'');
        }
        if (xmlhttp) {
            var url;
            var lan_ip=ip;
            var temp_cURL = document.URL.split("/");
            var mURL = temp_cURL[2];
            if(mURL == lan_ip){
                    url="http://"+lan_ip+"/lang_en.xml";
            }else{
                    url="http://"+mURL+"/lang_en.xml";
            }
            xmlhttp.onreadystatechange = parseXML;
            xmlhttp.open("GET", url, true);
            xmlhttp.send(null);
        }
    }
    function createRequest()
    {
        var XMLhttpObject = null;
        try {
            XMLhttpObject = new XMLHttpRequest();
        } catch (e) {
            try {
                XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    return null;
                }
            }
        }
        return XMLhttpObject;
    }
}
</script>
</head>
<body topmargin="1" leftmargin="0" rightmargin="0" bgcolor="#757575">
<script>
    var mydata = new myObj(''ipaddress'', ''a'');
    mydata.myxml();
    mydata.DisplayInfo();
</script>
</body>
</html>





我的xml如下所示





my xml is as bellow

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelop/" env:encodingStyle="http://www.w3.org/2003/05/soap-encoding/"><env:Body>
<a><![CDATA[ Success ]]></a>
</env:Body></env:Envelope>

推荐答案

Anderson710129写道:
Anderson710129 wrote:

this.DisplayInfo = Display; this.myxml = do_xml;

this.DisplayInfo = Display; this.myxml = do_xml;


Anderson710129写道:
Anderson710129 wrote:

xmlhttp.open("GET",url,true);

xmlhttp.open("GET", url, true);


更改这些语句的顺序.提取结果后使用显示.您将XHR设置为Async(第三个参数为true)...因此,使用它时可能未返回响应.


change the order of these statements. Use display after fetching the result. You set the XHR as Async (3rd parameter as true)... thus, the response might had not returned when you use it.

xmlhttp.responseXML.getElementsByTagName(key)


为此发出警报,然后检查您得到了什么. xml是否正确返回,以便提取标记?然后还要检查值.

如果仍然存在问题,请告诉我们您的发现.


Put an alert for this and check what you get. Is the xml returned correctly such that the tag is extracted? And then also check for value.

If it''s still an issue, tell us what you find.


这篇关于[XML和Javascript]我无法获取XML数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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