public JsonResult JsonData()
{
HttpContext.Response.AppendHe
return Json(db. Weathers.ToList());
}
方法有一個重構:
protected internal JsonResultJson( object data); protected internal JsonResult Json(object data, JsonRequestBehavior behavior);##我們只需要使用第二種就行了,加上一個json
請求行為為Get方式就OK了public JsonResult GetPersonInfo() { var person = new { Name = "張三", Age = 22, Sex = "男" };
returnJson(person,JsonRequestBehavior.AllowGet); }
這樣一來我們在前端就可以使用Get方式要求了: 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>
以上是關於json result的實例代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!