Javascript call to a WCF method

Suppose we have a WCF method with URL ‘http://myserver/myservice.svc’ and a method MyMethod which expectes a parameter para1. We can call this WCF method using XMLHTTP, by using the following code:

var xmlHttp3 = new ActiveXObject(‘Microsoft.XMLHTTP’);
xmlHttp3.open(‘get’, ‘http://myserver/myservice.svc//MyMethod?para1=’ + value, false);
xmlHttp3.send();

Another method of call would be through jquery

function AjaxSend(url, data) {

/// <summary>

/// Sends request to WCF method<br/>The format of the url and data can be customised on the WCF side by specifying the attribute UriTemplate for the WebInvoke method decoration

/// </summary>

/// <param name=”url”>URL of WCF method<br/>

Example: http://MyServer/myservice.svc/MyMethod</param>

/// <param name=”data”>Parameters to pass to the WCF method<br/>

Example: para1=value</param>

/// <returns>Response object from the WCF method</returns>

var response;

$.ajax({

type: “GET”, //GET or POST or PUT or DELETE verb

url: url, // Location of the service

data: data, //Data sent to server

contentType: “application/json; charset=utf-8”, // content type sent to server

dataType: “json”, //Expected data format from server

processdata: true, //True or False

async: false,

success: function (msg) {//On Successfull service call

response = msg;

},

error: function (result) {// When Service call fails

alert(‘Service call failed: ‘ + result.status + ‘ – ‘ + result.statusText);

if (result.responseText) {

var err = result.responseText;

if (err)

error(err);

else

error({ Message: “Unknown server error.” })                 }

return;

}

});

return response;

}

Advertisements
This entry was posted in Helpers, Javascript and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s