somebody
somebody

Reputation: 1107

How to create request with SOAP message with JavaScript?

I have a Web Service and I want to call some method. For this I create:

<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>

But every time I have error parseerror with "Cannot read property 'documentElement' of null". But when I call my service from Google Chrome application "Postman", I haven't error. How to create SOAP request to my Web Service using Java Script?

Upvotes: 0

Views: 506

Answers (1)

Pasupathi Rajamanickam
Pasupathi Rajamanickam

Reputation: 2052

This will only work in 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>

Upvotes: 1

Related Questions