Home > Article > Backend Development > Example code about json result
public JsonResult JsonData()
db. Weathers.ToList());
}
json
The method has a refactoring:
protected internal JsonResult Json
( object data); protected internal JsonResultJson(object data, JsonRequestBehavior behavior);We only need to use the second one, plus a jsonIt’s OK if the request behavior is Get method
public JsonResult GetPersonInfo() { var person = new { Name = "Zhang San", Age = 22, Sex = "Male" }; return
Json(person,JsonRequestBehavior.AllowGet); } In this way we can use it on the front end Get method requested:
view
##$.ajax({ url: "/FriendLink/ GetPersonInfo", type: "POST", dataType: "json", data: { }, success: function(data) { $("#friendContent").html(data.Name); } } )
<!DOCTYPE html><html><head runat="server"><title>Index2</title><script src="\Scripts\jquery-1.10.2.min.js?1.1.11" type="text/javascript"></script><script type="text/javascript">var login = function () {
$.ajax({ type: "post", url: "http://localhost:4968/Weathers/JsonData", data: null, success: function (res) {
alert(JSON.stringify(res));
}, dataType: "json"});
}</script></head><body><div id="nav"><a href="/Home/Index">ajax+Handler</a> <a>ajax+action</a></div><div><h3>Login</h3><button type="button" onclick="login()">Submit</button></div></body></html>
The above is the detailed content of Example code about json result. For more information, please follow other related articles on the PHP Chinese website!