Without further ado, here is a javascript class that can be used to call a WCF REST web service:
// ----------------------------------------
// ----------------------------------------
// Class WcfService
//
// Dependencies:
// JQuery
//
// ----------------------------------------
// Constants
// ----------------------------------------
WcfService.URL_LOCATION_LOCAL = document.location.protocol + "//" + document.location.host + "/";
WcfService.DATATYPE_JSON = 'json';
WcfService.CONTENTTYPE_JSON = 'application/json; charset=utf-8';
WcfService.MSG_PREFIX = 'WcfService.js, ';
// ----------------------------------------
// "Private" section
// ----------------------------------------
WcfService._getNowAsString = function()
WcfService.prototype._showDebugMessage = function( msg )
WcfService.prototype._callFailed = function ( xhr, statusCodeText, statusText )
WcfService.prototype._callSucceeded = function( result )
// Unwrap (.NET ".d") and deserialize result.
WcfService.prototype._unwrapResult = function( result )
//
// Method to call a WCF service
//
// Arguments:
// string type - GET, POST, PUT or DELETE verb
// string url - Location of the service, i.e.: "Service.svc/GetUser<";
// string data - Data sent to server, i.e.: ''
// string contentType - content type sent to server
// string dataType - Expected data format from server
// bool processData - True or False
//
WcfService.prototype._call = function ( type, url, data, contentType, dataType, processData, doSuccess, doError )
var jqxhrPromise = $.ajax( );
return jqxhrPromise;
}
// ----------------------------------------
// Constructor(s)
// ----------------------------------------
function WcfService()
// ----------------------------------------
// "public" section
// ----------------------------------------
WcfService.prototype.post = function ( url, data, doSuccess, doError )
WcfService.prototype.get = function ( url, data, doSuccess, doError )


Leave A Comment