日期:2014-05-17  浏览次数:20450 次

如何获取responseText中的result值?
用异步方式,获得webservice的一个返回值,responseText是一段XML,无法将其中结果getNumResult提取出赋值给textbox.innerText,请问各位大虾该如何处理?

返回的responseText:


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <soap:Body>
        <getNumResponse xmlns="http://tempuri.org/">
            <getNumResult>1748</getNumResult>
        </getNumResponse>
    </soap:Body>

</soap:Envelope>




js代码:

        function RequestWebService() {
            //这是我们在第一步中创建的Web服务的地址
            var URL = "http://localhost:1748/WebSite2/Service.asmx";

            //在这处我们拼接
            var data;
            data = '<?xml version="1.0" encoding="utf-8"?>';
            data = data + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
            data = data + '<soap12:Body>';
            data = data + '<getNum xmlns="http://tempuri.org/" />';
            data = data + '</soap12:Body>';
            data = data + '</soap12:Envelope>';

            //创建异步对象
            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            xmlhttp.Open("POST", URL, false);
            xmlhttp.SetRequestHeader("Content-Type", "application/soap+xml");
            xmlhttp.Send(data);
            var str = xmlhttp.responseText;

            // 下面这句返回的是一段XML,而不是想要的getNumResult节点的值
            document.getElementById("Text1").innerText = str;

            // 下面写会找不到该节点
            var xmlDoc = xmlhttp.responseXML
            var str2 = xmlDoc.selectSingleNode("Envelope\Body\getNumResponse\getNumResult").