如何使用JavaScript创建带有SOAP消息的请求

How to create request with SOAP message with JavaScript?

本文关键字:SOAP 消息 请求 何使用 JavaScript 创建      更新时间:2023-12-13

我有一个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时,都会出现"无法读取null的属性'documentElement'"。但当我从谷歌Chrome应用程序调用我的服务"邮差"时,我没有出错。如何使用Java脚本创建对Web服务的SOAP请求?

这只适用于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>