Home  >  Article  >  Backend Development  >  Example code about json result

Example code about json result

零下一度
零下一度Original
2017-06-24 09:25:402004browse

public JsonResult JsonData()
                                                                                                db. Weathers.ToList());
}


json
The method has a refactoring:

protected internal JsonResult Json

( object data); protected internal JsonResult

Json(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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn