简体   繁体   English

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

[英]How to create request with SOAP message with JavaScript?

I have a Web Service and I want to call some method. 我有一个Web服务,我想调用一些方法。 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". 但是,每当我parseerror “无法读取null的属性'documentElement'”错误parseerror时。 But when I call my service from Google Chrome application "Postman", I haven't error. 但是,当我从Google Chrome应用程序“邮递员”中调用服务时,我没有出错。 How to create SOAP request to my Web Service using Java Script? 如何使用Java脚本向我的Web服务创建SOAP请求?

This will only work in IE. 这仅在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM