关于json result的实例代码

关于json result的实例代码,第1张

关于json result的实例代码 public JsonResult JsonData()
{
HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");


return Json(db.Weathers.ToList());
}

json方法有一个重构:

protected internal JsonResult Json(object data); protected internal JsonResult Json(object data, JsonRequestBehavior behavior);

我们只需要使用第二种就行了,加上一个 json请求行为为Get方式就OK了

public JsonResult GetPersonInfo() { var person = new { Name = "张三", Age = 22, Sex = "男" }; return Json(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>&nbsp; <a>ajax+action</a></div><div><h3>Login</h3><button type="button" onclick="login()">Submit</button></div></body></html>

以上就是关于json result的实例代码的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/684207.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存