0

I can get data from an entity framework into a javascript grid (SlickGrid). The grid gets the data from a WCF framework with Ajax. This works great, but now I want to send an object back to the WCF service. If I debug the service I see the ajax can access the function. If I check the object the object is empty. In the console I can see I send an object(check screenshot). How can I catch the object so I can read the data?

This is how I tried it WCF:

[OperationContract]
public void Sting(PreEmeaData postData)
{
    var x = 1; //Breakpoint, postData is null?
}

JavaScript:

function sendDataToWcf(object) {
    $.ajax({
        type: "POST",
        url: "DataService.svc/Sting",
        data: JSON.stringify(object),
        processData: false,
        contentType: "application/json",
        dataType: "json",
        success: suckcess,
        error: showError
    });
}

enter image description here

Example of the class PreEmeaData:

[DataContract]
public class PreEmeaData
{
    [DataMember]
    public string BO { get; set; }
    [DataMember]
    public string Agreement { get; set; }

}

Update, apparently there is a difference between the data I send and the data I receive. This is how I send it from the WCF to the Ajax:

[{"BO":"NL", "Agreement":"201012230314MA"}]

This is what I try to send whith Ajax:

{"__type":"PreEmeaData:#TPlatform","Agreement":"201012230314MA","BO":"NL"}

I found out what I have send with the following code:

            IEnumerable<PreEmeaData> list =  newData; //I send the object out like this thats why I use IEnumerable
            var serializer = new JavaScriptSerializer();
            var serializedResult = serializer.Serialize(list);

I found out to console the data before I send it with ajax:

console.log(JSON.stringify(object, null, null));
4
  • Looking at the WCF I created not too long ago. I also have [WebInvoke(Method="POST", ResponseFormat = WebMessageFormat.Json)] above the method. Are you getting any errors in the console? Commented Dec 12, 2013 at 21:55
  • No I dont get any error. He just comes in the server side function and he runs the code in this function. But the object is NULL Commented Dec 12, 2013 at 21:58
  • Also, what is: PreEmeaData, you may want to get a simple String to work first. Commented Dec 12, 2013 at 22:06
  • PreEmeaData is my class that has exactly the same columns as the columns in the grid. I also use this class to send them to the grid, this works. They are all strings I check the datatype on the Javascript side. I added an example of the class. Commented Dec 13, 2013 at 7:53

2 Answers 2

1

Add DataContract and Serializable attributes to PreEmeaData, and make sure your JSON matches your class definition. A way to test that is, to stringify your entity on the server side using JavascriptSerializer and then use that string to test out your AJAX calls. That always worked for me.

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

2 Comments

Your right I receive different data than I sent. I have no idea how I could change this. I updated the post for the 2 different feeds
Where is the object, that is parameter of sendDataToWcf, coming from ?
0
data: JSON.stringify({ postData: fooObject}),

I changed the data to this. postData is the variable I gave to the object in the WCF code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.