Jquery - Javascript DHTML - Developer Resourses - JQuery Ajax Call Passing And Receiving JSon ::: Lionfish Tehcnologies



Home / Developers Resourses / Tips

JQuery Ajax Call Passing And Receiving JSon

Client Side (Javascript)


function Save(){
// Build the json object with the array of hobbies
var jsonObjects = {
  Name: $("#txtName").val(),
  DOB: $("#txtDOB").val(),
  Phone: $("#txtPhone").val(),
  Email: $("#txtEmail").val(),
  Hobbies: []
};

// Fill the array of hobbies
for (var i = 0; i < hobbiesArray.length; i++){
  jsonObjects.Hobbies.push({
    HobbyId: hobbiesArray[i].Id,
    HobbyName: hobbiesArray[i].Name
  });
}

$.ajax({
  type: "POST",
  url: g_strWebServiceURL + "/SavePerson",
  data: "{ person:" + JSON.stringify(jsonObjects) + "}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: Save_Callback,
  error: OnErrorCall
});
}
function Save_Callback(results){
var resJSon = window.JSON.parse(results.d);
alert(resJSon.root.newId);
}



WebService Side (C# .NET)

[WebMethod(true)]
public string SavePerson(Person person)
{
...
}

[Serializable]
[XmlType]
public class Person
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public datetime DOB { get; set; }
[XmlAttribute]
public string Phone { get; set; }
[XmlAttribute]
public string Email { get; set; }

public List Hobbies;
}



Lionfish Technologies ® 2009 All rights reserved