Search code examples
javascriptweb-servicessoapcross-domainxdomainrequest

How to implement a SOAP call using XDomainRequest in IE8


I'm trying to call a WCF service using soap in IE8 and I can't seem to get my head around how xdr's work. Can they be used to pass xml to a soap endpoint?


Solution

  • Can they be used to pass xml to a soap endpoint?

    We are basically talking about a POST request with a properly formatted XML SOAP payload and you can sure do that with the XDomainRequest object. Here is a basic example:

    <script type="text/javascript">
        var xdr;
    
        function soapTest() {
            var data =  '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
                        '   <soapenv:Body>' +
                        '      <tem:Add>' +
                        '         <tem:a>1</tem:a>' +
                        '         <tem:b>2</tem:b>' +
                        '      </tem:Add>' + 
                        '   </soapenv:Body>' +
                        '</soapenv:Envelope>';
    
            if (window.XDomainRequest) {
                xdr = new XDomainRequest();
                if (xdr) {
                    xdr.onerror = function() { alert("an error occured"); };
                    xdr.ontimeout = function() { alert("timeout"); };
                    xdr.onload = function() { alert(xdr.responseText); }
                    xdr.timeout = 10000;
                    xdr.open("post", "http://SomeServer/SoapCalculator");
                    xdr.send(data);
                } else {
                    alert("Failed to create XDR");
                }
            } else {
                alert("XDR doesn't exist");
            }
        }
    </script>
    

    The payload in the example is for a web service exposing this contract. You could create a mock web service based on that WSDL on a server that responds with the Access-Control-Allow-Origin header. Then, if all goes well you should see a response like this:

    enter image description here

    You can then parse the response to extract the data you need instead of printing the raw string.

    For troubleshooting also have a look at the following post: XDomainRequest - Restrictions, Limitations and Workarounds.