How to serialise an object to json and send it to a web service?
var object = something....
function BindJson() {
$.ajax({
type: "POST",
url: "NewPage.aspx/GetJson",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function (data) {
}
})
}
<body onload="BindJson();">
Server:
[WebMethod]
public static string GetSerializedJsonObject()
{
return "";
}
This will work for you (full working code sample below). The key is to pass in a Person object. Also, I used a simple web service (myService.asmx) instead of an aspx page. Why bother with the extra overhead if it isn’t needed?
The key is, on the client, create a Person object and then use JSON.stringify to pass the Person object to the webservice.
Javascript
C#