关于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>

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...