1

I'm using the following straight-forward jQuery implementation

    var soapMessage = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
    <soap:Body xmlns:ns1="http://site.com/">\
        <ns1:insertLocationElement>\
            <ns1:location>\
                <ns1:locationName>' + sName + '</ns1:locationName>\
                <ns1:locRadius>' + sRadius + '</ns1:locRadius>\
                <ns1:lngGPS>' + gLongitude + '</ns1:lngGPS>\
                <ns1:latGPS>' + gLatitude + '</ns1:latGPS>\
                <ns1:locationAddress>' + sAddress + '</ns1:locationAddress>\
            </ns1:location>\
        </ns1:insertLocationElement>\
    </soap:Body>\
</soap:Envelope>';

    $.ajax({
        type : "POST",
        url : service,
        data : soapMessage,
        contentType : "text/xml; charset=ansi",
        dataType : "xml",
        success : AjaxSucceededInsert,
        error : AjaxFailedGeneral
    });

I was wondering - is there any other way to call such web services, without including the whole SOAP envelope? it makes the code utterly unclean IMO.

2 Answers 2

1

The SOAP protocol message format defines 3 elements: Envelope, Header and Body. Of the three, the Header is optional and can be omitted, the other two are mandatory (SOAP 1.1 Specification: 4. SOAP Envelope, SOAP 1.2 Specification: 5. SOAP Message Construct).

Thus, You can't completely get rid of the Envelope element, as it is required by the SOAP protocol message format. But, You can use some ready-made library that could prepare the message for You implicitly, so that You don't have to deal with it. It will also allow You to keep the SOAP message construction code in one place somewhere deep where You don't see it. And it will definitely make Your code cleaner.

A quick googling tells that there is a jQuery Plugin called jqSOAPClient available to us, but unfortunately, the jQuery Plugins website is off-line at the moment. Other alternative is JavaScript SOAP Client.

1

there is a relatively new plugin available:

http://plugins.jquery.com/soap/

I forked the project, and have been working on some modifications (the plugin did not handle the service I was working with). I hope to get my updates merged at some point, but would be happy to have any feedback.

https://github.com/zachofalltrades/jquery.soap

The really nice thing about this plugin is that it will build the XML for you. You provide a JSON data structure, and just few additional parameters. The plugin build the XML, sends it off to the service, and converts the response back to JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.