Because of the protection of data in ASP. 2, the request is post by default, so it is required to post requests when the front-end is requested
Action method:
Public jsonresult getpersoninfo () { varnew { " Zhang San ", A , " male " }; return Json (person); }
Front-End Request code:
$.ajax ({ "/friendlink/getpersoninfo", "POST " , "json", data: {}, success: function (data) { $ ("#friendContent"). HTML (data. Name); } )
However, if you change the Get method request, you will get an error, such as:
Isn't it possible to request it in a get way?
Of course it can be, it's simple.
The JSON method has a refactoring:
protected internal Jsonresult Json (object data);
protected internal Jsonresult Json (object data, jsonrequestbehavior behavior);
We just need to use the second type, plus a JSON request behavior is OK for Get mode
Public jsonresult getpersoninfo () { varnew { " Zhang San ", A , " male " }; return Json (person,jsonrequestbehavior.allowget); }
In this way we can request it in the front-end using get:
$.getjson ("/friendlink/getpersoninfo"null, function (data) { $ ("#friendContent"). HTML (data. Name); })
So that we can serialize the objects we want to return through JSON (return jsonresult), we don't have to use JavaScriptSerializer to serialize, MVC has helped us with these, isn't it easier now!
Jsonresult Usage of MVC