如何使用JavaScript创建SOAP消息请求?

时间:2015-02-16 07:34:08

标签: javascript web-services soap

我有一个Web服务,我想调用一些方法。为此,我创建:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#btnCallWebService").click(function (event) {
            var wsUrl = "http://localhost:8080/services/StockQuoteService/";

            var soapRequest =
'<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" >' +

 '<soap:Body xmlns:m="http://sample/">'+
 ' <m:getPrice>'+
 '    <m:symbol>IBM</m:symbol>'+
 ' </m:getPrice>'+
 '</soap:Body>'+
'</soap:Envelope>';

            $.ajax({
                type: "POST",
                url: wsUrl,
                contentType: "text/xml",
                dataType: "xml",
                data: soapRequest,
                success: processSuccess,
                error: processError
            });

        });
    });

    function processSuccess(data, status, req) {
        if (status == "success")
         $("#response").text($(req.responseXML).find("HelloResult").text());
    }

    function processError(data, status, req) {
        alert(req.responseText + " " + status);
    }  

</script>
</head>
<body>
    <input id="btnCallWebService" value="Call web service" type="button" />
    <div id="response" />
</body>
</html>

但每次我的错误parseerror都带有&#34;无法读取属性&#39; documentElement&#39; of null&#34;。但是,当我从谷歌Chrome应用程序调用我的服务&#34; Postman&#34;时,我没有错误。如何使用Java Script为我的Web服务创建SOAP请求?

1 个答案:

答案 0 :(得分:1)

这只适用于IE。

<script language="JavaScript">
var request = new XMLHttpRequest();
request.open('POST', 'http://www.webserviceX.NET//CurrencyConvertor.asmx',true);
var m_request = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ConversionRate xmlns="http://www.webserviceX.NET/"><FromCurrency>INR</FromCurrency><ToCurrency>USD</ToCurrency>    </ConversionRate>  </soap:Body></soap:Envelope>'
request.setRequestHeader('Content-Type', 'text/xml');
request.send(m_request);
request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
        alert("VALUE" + request.responseText);
    }
}
</script>