Ajax is the process of assembling and parsing a complex data that passes through its own format, so there is a de facto
Standard JSON for data transfer.
JSON serializes a complex object into a string, and then deserializes the string into an object that JavaScript can read in the browser side.
Look at the JSON format. JSON is supported in almost all languages.
C # will. NET object is serialized as a JSON string method:
JavaScriptSerializer (). Serialize (P), JavaScriptSerializer in System.Web.Extensions.dll,
is the new class in the. net3.x, if the. Third-party components are required in Net2.0.
JQuery Ajax gets data in JSON format, using the $.parsejson (data) method to parse JSON-formatted data into JavaScript objects
A function can pass "JSON" after the post function, and data is the object after deserialization, eliminating the Parsejson
Case: Returning a class to the client with JSON using the Jqurey
1. Processing page Settings Json1.ashx
public void ProcessRequest (HttpContext context) { context. Response.ContentType = "Text/plain"; JavaScriptSerializer JSS = new JavaScriptSerializer (); Create JavaScriptSerializer string json = JSS. Serialize (new person () {name= "Xgao", age=18}); Context. Response.Write (JSON); } public class person {public string Name {get; set;} public int Age {get; set;} }
2.JAVAscript settings
<script type= "Text/javascript" > $ (function () { $.post ("Json1.ashx", function (data, state) { if (state = = "Success") { var person = $.parsejson (data); Use Parsejson to convert alert (person. Name); Alert (person. (age) ;}); </script>
C # using JSON