Javascript在发布后获得响应值

时间:2013-07-29 10:22:22

标签: javascript web-services soap

我正在试图发布网络服务。但我希望得到回应价值。 我的代码如下所示:

 var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', 'http://www.w3schools.com/webservices/tempconvert.asmx',  true);

        // build SOAP request
        var sr =
            '<soapenv:Envelope' + ' ' +
                'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
                'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
                '<soapenv:Body>' +
                    '<CelsiusToFahrenheit  xmlns="http://tempuri.org/">' +
                    '<Celsius>44</Celsius>' +
                    '</CelsiusToFahrenheit>'+
                '</soapenv:Body>' +
            '</soapenv:Envelope>';

        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {

                    alert('done use firebug to see response');
                }
            }
        }
        // Send the POST request
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp.send(sr);

当我查看firebug时,我意识到Web服务器的响应值是:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CelsiusToFahrenheitResponse xmlns="http://tempuri.org/"><CelsiusToFahrenheitResult>111.2</CelsiusToFahrenheitResult>   </CelsiusToFahrenheitResponse></soap:Body></soap:Envelope> 

但我不知道我怎么能得到     111.2价值?

1 个答案:

答案 0 :(得分:1)

您正在从ajax调用中获取xml响应。做得正确,你需要解析xml代码。

您可以使用jQuery轻松完成:

var r = $(this.responseText).find("CelsiusToFahrenheitResult").text();