Click here to Skip to main content
15,037,178 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Consume WCF REST Service in our application with client to server end call using ajax and json object and deserialize json object...
Posted
Comments
[no name] 10-Jul-14 9:11am
   
Did you Googled?

1 solution

   
Comments
Member 10619915 11-Jul-14 5:12am
   
Sir, it's not solution of my issue.
Basically In my application i am sending json object in .cs or .ashx file and now i want deserialize it object then call wcf rest service and save data and return response in success or in label message.

Right Now i have send json object and also deserialize it in .cs or .ashx file but not call wcf rest service from here ...

I am sending useing code please review and give your solution

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

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