Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
In our project, i have taken two class library , one class library for wcf rest service (it's build succeeded) and one for Client application. my requirement is that i want insert data on button click so using $.ajax and send json object in handler file (In handler file json object deserialize in class object) after that i want consume wcf service and take response.


Using following code please suggest me how to call wcf service in handler file

Step 1:
on button click call this function first...

function AddNewCountry() {
var countryId = $('#txtCountryId').val();
var countryName = $('#txtCountryName').val();
var active = $("#chkActive").attr("checked") ? true : false;

if (countryId == null || countryId == "") {
alert("Please enter country id")
return;
}
if (countryName == null || countryName == "") {
alert("Please enter country name")
return;
}
if (active == false) {
alert("Please check active")
return;
}

var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();
var dateOutput = d.getFullYear() + '-' + (('' + month).length < 2 ? '0' : '') + month + '-' + (('' + day).length < 2 ? '0' : '') + day + ' ' + (('' + hour).length < 2 ? '0' : '') + hour + ':' + (('' + minute).length < 2 ? '0' : '') + minute + ':' + (('' + second).length < 2 ? '0' : '') + second;

var _objCountryData = new Object();
_objCountryData.CountryId = $('input:text[name=CountryId]').val();
_objCountryData.CountryName = $('#txtCountryName').val();
_objCountryData.Active = active;
//_objCountryData.DeActivateDate = "\/Date(1283219926108)\/";
_objCountryData.EntryBy = "super.admin";
_objCountryData.EntryDate = dateOutput;

var CountryData = JSON.stringify(_objCountryData);

var url = "../MyHandler.ashx";

$.ajax({
type: "POST",
url: url,
data: CountryData,
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (data, status, jqXHR) {
debugger;
alert("success......" + data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}

Step 2: call MyHandler.ashx


public class MyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
RestService.MODEL.CustomCountryMaster _objCountry = new RestService.MODEL.CustomCountryMaster();
context.Response.ContentType = "application/json";
var data = context.Request;
var sr = new StreamReader(data.InputStream);
var stream = sr.ReadToEnd();
//Converting first json object into class object
var javaScriptSerializer = new JavaScriptSerializer();
_objCountry = javaScriptSerializer.Deserialize(stream);


// Now i want call wcf rest resvice and send this country object
// wcf service is exist in one more class libaray (like : WCFRestService)



}
catch (Exception msg) { context.Response.Write(msg.Message); }

}

public bool IsReusable
{
get
{
return false;
}
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900