Please see the code below:
<head runat="server">
<title></title>
<script type="text/javascript" src="Javascript/json2.js"></script>
<script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script>
<script type = "text/javascript">
Test();
function Test() {
alert("got here 1");
$.ajax({
type: "POST",
url: "AjaxObjectTest.aspx/Test",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess(),
async: false,
failure: function (response) {
alert('there was an error counting possibles')
}
});
function OnSuccess() {
return function (response) {
var data = response.d;
alert(data.name);
}
}
}
</script>
</head>
The asp.net code looks like this:
Public Class Person
Public id As Integer
Public age As Integer
Public name As String
End Class
Public Class AjaxObjectTest
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()> _
Public Shared Function Test() As Person
Dim p1 As Person = New Person
p1.id = 1
p1.age = 34
p1.name = "Mark"
Return p1
End Function
End Class
Please see this link: https://msdn.microsoft.com/en-us/library/ms733127(v=vs.110).aspx
I don't have to add a DataMember attribute to the Person class and I do not have add a Data Contract attribute to the attributes i.e. Person.ID, Person.Age and Person.Name in order for it to work.
Why is this?