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;
}
}
}