How to fill an xml (request of a WS SOAP) with Javascript

Asked

Viewed 2,118 times

2

I have an Ajax function doing a POST for a SOAP webservice and I need to insert values into the request XML elements based on what the page user inserts into an input text. How can I do that?

My script looks like this:

var soapMessage = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://teste.transaction.service">\
                <SOAP-ENV:Header>\
                <ns1:testeHeader>\
                <channel>Portal</channel>\
                </ns1:testeHeader>\
                </SOAP-ENV:Header>\
                <SOAP-ENV:Body>\
                <teste:VC>\
                <teste:VCRequestHeader>\
                <PIN>1234</PIN>\
                </teste:VCRequestHeader >\
                </teste:VC_WSLOGIN>\
                </SOAP-ENV:Body>\
                </SOAP-ENV:Envelope>';
            $.ajax({
                type: "POST",
                dataType: "xml",
                url: wsUrl,
                data: soapMessage,
                crossDomain: true,
                success: function(data) {
                    console.log(data);
                },
                error: function(error) {
                    console.log(error);
                }
            });

Thanks in advance for your attention.

  • has tried to parser? var parser = new DOMParser();&#xA;var soapMsg = parser.parseFromString(soapMessage, "text/xml");

1 answer

2


To do this first you will need:

1 - First: Declare HTML field

<input type="text" id="cidade"/>

2 - Take the xml schema used in the webservice

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.webserviceX.NET" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <SOAP-ENV:Body>
        <tns:GetWeather xmlns:tns="http://www.webserviceX.NET">
            <tns:CityName></tns:CityName>
            <tns:CityName></tns:CityName>
            <tns:CountryName></tns:CountryName>
            <tns:CountryName></tns:CountryName>
        </tns:GetWeather>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

3 - Calling the Js method

var cidade = $("#cidade").val();
var soapMessage = 
    "<?xml version='1.0' encoding='UTF-8' standalone='no'?>" +
    "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope' xmlns:tm='http://microsoft.com/wsdl/mime/textMatching/' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' xmlns:mime='http://schemas.xmlsoap.org/wsdl/mime/' xmlns:tns='http://www.webserviceX.NET' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:s='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://schemas.xmlsoap.org/wsdl/soap12/' xmlns:http='http://schemas.xmlsoap.org/wsdl/http/' xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
        "<SOAP-ENV:Body>" +
            "<tns:GetWeather xmlns:tns='http://www.webserviceX.NET'>" +
                "<tns:CityName>" + cidade + "</tns:CityName>" +
                "<tns:CityName>" + cidade + "</tns:CityName>" +
                "<tns:CountryName></tns:CountryName>" +
                "<tns:CountryName></tns:CountryName>" +
            "</tns:GetWeather>" +
        "</SOAP-ENV:Body>" +
    "</SOAP-ENV:Envelope>";

var wsUrl = "http://www.webservicex.com/globalweather.asmx?wsdl";
$.ajax({
    type: "POST",
    dataType: "xml",
    url: wsUrl,
    data: soapMessage,
    crossDomain: true,
    success: function(data) {
        console.log(data);
    },
    error: function(error) {
        console.log(error);
    }
});
  • Thank you very much, Emir! This is exactly what I was looking for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.