Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@kerrishotts

kerrishotts Sep 26, 2012

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

Owner

kerrishotts commented Sep 26, 2012

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

@saraadel

This comment has been minimized.

Show comment
Hide comment
@saraadel

saraadel Dec 24, 2012

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

saraadel commented Dec 24, 2012

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