I have a method on the server-side that retrieves user data from a SQL Server database using EF 6. The data is then serialized using the Json.NET framework and returned to the client without filtering out references to other tables, requiring me to use the "PreserveReferencesHandling" option.
On the client-side, AJAX is used to call this server-side method. However, I am facing difficulties extracting the data from the serialized object on the client-side even though it is delivered to the browser as expected.
Here is an excerpt of the server-side method:
[WebMethod]
public static string ContactData(long id)
{
IService<Contact> contactService = new ContactService();
Contact contactEdit = contactService.GetById(id);
string str = JsonConvert.SerializeObject(contactEdit, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
return str;
}
And here is the relevant part of the client-side function:
$.ajax({
type: "POST",
url: "Contacts.aspx/ContactData",
data: JSON.stringify({ id: contactId }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (contact) {
$("#tbFirstName").val(contact.d[0].Contact.first_name);
},
});
In Visual Studio's JSON Visualizer, the JSON object for testing purposes looks like this:
While in the browser (Firebug), the received JSON object appears like this: Firebug
If more information is required, please let me know.