I typically would use JSON as my AJAX data type. JSON (JavaScript Object Notation) it works much like XML but it uses about 70% less data. You will also find it easier to parse in your javascript. You don't have to initialize date/number objects from xml!
{ "people": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" }
]}
Thanks IBM For The Example
<people>
<person>
<firstName>Brett</firstName>
<lastName>McLaughlin</lastName>
<email>[email protected]</email >
</person>
<person>
<firstName>Jason</firstName>
<lastName>Hunter</lastName>
<email>[email protected]</email >
</person>
<person>
<firstName>Elliotte</firstName>
<lastName>Harold</lastName>
<email>[email protected]</email >
</person>
</people >
I have found the MVC JSON framework has some shortfalls, in regards to Serialization, De serialization, & Ignore Member Attributes. I have found Json.NET project on codeplex. it seams to fill all the functionality gaps that MVC JSON doesn't cover.
So in your controller, i am using Json.NET, please note that the output is ContentResult and it Does Not have a View
public ContentResult ProcessRequestAction(string Email, string Password)
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
return Content(JavaScriptConvert.SerializeObject(product));
}
Typical JQuery Code, for JSON request
$.ajax({
type: "POST", /* GET OR POST */
url: "JSON_MVC_URL_HERE", /* your url here */
dataType: "json", /* json or xml */
data: null, /* JSON CODE HERE TO SET GET OR POST BACK PARAMS */
success: function(data){
alert(data.Name); /* Gets Name Element */
alert(data.Expiry); /* Gets Expiry Element */
alert(data.Price); /* Gets Price Element */
jQuery.each(data.Sizes, function() { /* Get Each Size */
alert(this);
});
}
});
$.getJSON also works but lacks http request type POST/GET