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.
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.