Reputation: 970
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
});
}
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));
Upvotes: 0
Views: 94
Reputation: 970
data: JSON.stringify({ postData: fooObject}),
I changed the data to this. postData is the variable I gave to the object in the WCF code.
Upvotes: 0
Reputation: 232
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.
Upvotes: 1