1

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

3 Answers 3

2

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

       var jsonList=JSON.stringify(listOfObjects);
       var dataToSend = '{"myEntity":'+jsonList+'';

        //Saving info
        $.ajax({
            type: "POST",
            async: false,
            data: dataToSend,
            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; }
}

What i am missing was

 1.var jsonList=JSON.stringify(listOfObjects);
 2.var dataToSend = '{"myEntity":'+jsonData+''; 

The 2nd point myEntity key should be there in the final json object.And the key should be same as the wcf method object parameter.

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

This answer really helped me, but for anyone else looking at it there is a mistake in the javascript. var dataToSend = '{"myEntity":'+jsonData+''; should be var dataToSend = '{"myEntity":'+jsonList+'}';
1

Try BodyStyle = WebMessageBodyStyle.Bare for your server method or introduce a root element named "myEntity" in your JSON data prior to calling JSON.stringify in your client code.

2 Comments

Whenever i am trying Bare then {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"} is coming
yes Oliver u r right.i included a root element and it worked.Thanks a lot.
0

May be this one is the error:

//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);
   } //-------------------^--------------shouldn't it be capital M

and in the ajax function:

$.ajax({
        type: "POST",
        async: false,
        data: {data : JSON.stringify(listOfObjects)}, //<---not quite sure about it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.