Hi all I am using jQuery to call a WCF service and on the client i am using Data Transfer Objects. Somehow my json is formed correct and when the call reaches my service the passed object is null, i know there is some silly mistake can you help me out with that. According to a Rick Starlah post i have configured my service and removed everything from the web.config and i am using in my service. I removed every other thing from the web.config as i am using System.ServiceModel.Activation.WebScriptServiceHostFactory. My web.config looks like this
My service looks like this
using System.ServiceModel;
using System.ServiceModel.Activation;
using CrimeReporter.Business;
[ServiceContract(Namespace = "GeneralService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
#if DEBUG
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
#endif
public class GeneralService
{
[OperationContract]
public void SaveCrime(Crime crime)
{
using (var crimeReporterDataContext = new CrimeReporterDataContext())
{
crimeReporterDataContext.Crimes.InsertOnSubmit(crime);
crimeReporterDataContext.SubmitChanges();
}
}
}
The function which i use to call the jquery $ajax looks like this
function ClientProxyDateFormatted(methodName, dto, successFunction, errorFunction) {
$.ajax({
type: "POST",
url: "GeneralService.svc/" + methodName,
contentType: "application/json; charset=utf-8",
data:JSON.stringify(dto),
dataType: "json",
processData: false,
success: function(msgg) {
var msg = JSON.parse(msgg, function(key, value) {
var a;
if (value != null) {
if (value.toString().indexOf('Date') >= 0) {
a = /^\/Date\((-?[0-9]+)\)\/$/.exec(value);
if (a) {
var dt = new Date(parseInt(a[1], 10));
return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}
}
return value;
}
});
successFunction(msg);
},
error: function(xhr, status, error) {
errorFunction(xhr, status, error);
}
});
return false;
}
On the default page i am using jQuery to pass DTO to my service like this.
<pre lang="C#">
<script type="text/javascript">
$(document).ready(function() {
$('#btnSubmit').click(function() {
var date = new Date();
var currentDate = date.getDate() + date.getMonth() + date.getFullYear();
var Crime = {};
Crime.CrimeName = $('#ddlCrimeName').val();
Crime.CrimePlace = $('#tbCrimePlace').val();
Crime.CityId = $('#ddlCrimeCity').val();
Crime.StateId = $('#ddlCrimeState').val();
Crime.DOC = currentDate;
Crime.TimeFrom = $('#tbCrimeTimeFrom').val();
Crime.TimeTo = $('#tbCrimeTimeTo').val();
var DTO = { 'Crime': Crime };
ClientProxyDateFormatted("SaveCrime", DTO, function(msg) { CrimeSaveSuccess(msg); },
function(xhr, status, error) { ErrorSavingCrime(xhr, status, error); });
});
});
function CrimeSaveSuccess(msg) {
alert("Inside Success");
}
function ErrorSavingCrime(xhr, status, error) {
alert("Inside Failure");
}
</script>
</pre>
Now in my service when i get crime variable it is always null, can you please suggest me some solution and tell me why is that happening.
Aashish Gutpa http://smallworkarounds.blogspot.com