In my application i want to save the data by using jQuery and Ajax.I have a WCF Service. I want to save a List of Objects by using ajax.I have tried with following code, but it is not working.
jquery code :
var listOfObjects=new Array();
//creating list of objects
for(var i=0;i<5;i++)
{ var MyEntity=new Object();
MyEntity.TestId =i;
MyEntity.TestId =i+"testName";
listOfObjects.push(MyEntity);
}
//Saving info
$.ajax({
type: "POST",
async: false,
data: JSON.stringify(listOfObjects),
url: "../ServiceLayer/myService.svc/SaveResults",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("success");
},
error: function () {
alert("Error");
}
});
WCF :
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public void SaveLabResults(List<MyEntity> myEntity)
{
var lstEntities=myEntity;
}
Entity:
[DataContract]
public class MyEntity
{
[DataMember]
public string TestId { get; set; }
[DataMember]
public string TestName { get; set; }
}
In this way i am trying to send the list data.But internal server error is coming.I am not getting where actually i am wrong.Is there any other way to send the list of objects to WCF ?
Thanks