<>
Controller controllers
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using System.web.script.serialization;namespace mvcapplication2.controllers{public class Homecontroller:controller { Public ActionResult Index () {return View (); }//---------------------------------The first notation: Server returns a JSON object public ActionResult GetData1 () { Jsonresult json = new Jsonresult (); Json. Jsonrequestbehavior = Jsonrequestbehavior.allowget; Accept the HTML GET request JSON from the client. Data = new {code = 0, message = "Success"}; return JSON; The direct return of a JSON object to client,client through the AJAX request, the direct is a JSON object, no need to use $.parsejson (data) This way to convert the string into a JSON object, Since the fetch itself is a JSON object. Instead of a string. }//---------------------------------Another way to do this: the server returns a JSON object public ActionResult GetData2 () { In fact, this method is called under the Jsonresult class: protected internal Jsonresult Json (object data); Note: JsOn () is a method in which the parameter is an object of type objects. The return value is Jsonresult type return Json (new {code = 0, message = "On"}); By testing: In such a form. The client can only fetch data through the asynchronous post request, assuming an asynchronous get request, unable to get the data}//---------------------------------The Third Way: Server returns a JSON-formatted string Public ActionResult GetData3 () {JavaScriptSerializer json = new JavaScriptSerializer (); var jsonobj = new {code = 1, message = "OK"}; Constructs an object string jsonstr = json. Serialize (Jsonobj); Convert this object to a JSON-formatted string return Content (JSONSTR); Returns a JSON-formatted string.So the client passes the AJAX request. The data obtained is a JSON-formatted string, so you need to use $.parsejson (data) to convert the string to a JSON object}}}
Views View
AJAX MVC Server returns JSON data, client gets JSON data