Skip to content

Instantly share code, notes, and snippets.

@kerrishotts
Last active June 14, 2016 16:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kerrishotts/3786070 to your computer and use it in GitHub Desktop.
Save kerrishotts/3786070 to your computer and use it in GitHub Desktop.
Simple SOAP library for JavaScript (PhoneGap Compatible), v0.1
/*jshint asi:true, forin:true, noarg:true, noempty:true, eqeqeq:false, bitwise:true, undef:true, curly:true, browser:true, devel:true, smarttabs:true, maxerr:50 */
/******************************************************************************
*
* SOAP
* Author: Kerri Shotts
*
* This library includes simple SOAP functions. MIT license.
*
*
* Usage is pretty straightforward:
*
* PKSOAP.performOperation ( { "host":"http://your.host:port", "url":"/path/to/your/services",
* "xmlns":"m", xmlnsurl":"http://url/to/your/service/namespaces",
* "operation": "theOperationToPerform",
* "params": { "param1": "value1", "param2": "value2" }
* },
* function ( success, data )
* { // success is TRUE if handled successfully. data will be an XML
* // document, ready to be handled using the typical DOM
* //methods, like getElementsByTagName, firstChild, nodeValue, etc.
* } );
*
* The operation is asynchronous, so don't assume the data is immediately available -- the client
* has to send the XHR, receive the data, parse the data, and then send it on to your callback method.
*
* Note: No checking is made of the data to ensure that the XML generated is valid. This will be in
* upcoming revisions.
******************************************************************************/
var PKSOAP = PKSOAP || {};
PKSOAP._postSoapEnvelope = function ( theWebServiceHost, theWebServiceURL, theSOAPEnvelope, completion )
{
var r = new XMLHttpRequest();
r.onreadystatechange = function()
{
if (r.readyState == 4)// loaded
{
if (r.status == 200 || r.status == 0)// success
{
if (completion)
{
console.log("success posting envelope, length " + r.responseText.length);
completion(true, r.responseText);
}
} else// failed to load
{
if (completion)
{
completion(false, r.status);
}
}
}
}
r.open('POST', "" + theWebServiceHost + theWebServiceURL, true);
r.setRequestHeader ("Content-Type","text/xml;charset=UTF-8");
r.setRequestHeader ("SOAPAction", "");
// may need to remove the following for Android 2.x
r.setRequestHeader ("Content-Length", theSOAPEnvelope.length);
r.setRequestHeader ("Host", theWebServiceHost.substr(theWebServiceHost.indexOf("://")+3));
r.setRequestHeader ("Connection", "Keep-Alive");
r.setRequestHeader ("User-Agent", "PKSOAP (iOS/PhoneGap)");
r.send(theSOAPEnvelope);
}
PKSOAP.performOperation = function(theOptions, completion)
{
var theWebServiceHost = theOptions["host"];
var theWebServiceURL = theOptions["url"];
var theWebServiceXMLNS= theOptions["xmlns"];
var theWebServiceXMLNSURL= theOptions["xmlnsurl"];
var theWebServiceOp = theOptions["operation"];
var theWebServiceParams=theOptions["params"];
// build our soap envelope
var theSOAPEnvelope = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:'+theWebServiceXMLNS+'="' + theWebServiceXMLNSURL + '">';
theSOAPEnvelope += '<soapenv:Header/>';
theSOAPEnvelope += '<soapenv:Body>';
theSOAPEnvelope += '<'+theWebServiceXMLNS+':' + theWebServiceOp + '>';
if (theWebServiceParams)
{
for (var theParam in theWebServiceParams)
{
var theParamValue = theWebServiceParams[theParam];
//TODO: need to worry about < and > in here....
theSOAPEnvelope += '<' + theParam + '>' + theParamValue + '</' + theParam + '>';
}
}
theSOAPEnvelope += '</'+theWebServiceXMLNS+':' + theWebServiceOp + '>';
theSOAPEnvelope += '</soapenv:Body>';
theSOAPEnvelope += '</soapenv:Envelope>';
PKSOAP._postSoapEnvelope( theWebServiceHost, theWebServiceURL, theSOAPEnvelope, function(success, data)
{
var theParsedData = {};
if (success)
{
try
{
theParsedData = ( new window.DOMParser() ).parseFromString(data, "text/xml");
} catch (err)
{
console.log("Failed to parse return XML");
success = false;
}
}
// call completion, if available
if (completion)
{
completion(success, theParsedData);
}
});
}
@kerrishotts
Copy link
Author

Note: Don't forget to change the user-agent string on line 61 if you are on a different platform or framework.

@saraadel
Copy link

There is a problem in the sample demo that you write please change the Square brackets to curley brackets like the following :
"params": [ "param1": "value1", "param2": "value2" ]
to
"params": { "param1": "value1", "param2": "value2" }

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment