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> <a>ajax+action</a></div><div><h3>Login</h3><button type="button" onclick="login()">Submit</button></div></body></html>
编辑推荐:
- 关于json result的实例代码02-21
- 字符串分割的使用实例代码02-21
- C# Invoke 和 BeginInvoke之间的区别详解02-21
- C# 5.0引入了两个关键字 --async和await02-21
- .net 2.0中的委托实例讲解02-21
- .net 1.x中的委托实例详解02-21
- 介绍一个力软敏捷开发框架02-21
- 什么是委托?总结.net中重要的特性委托02-21
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
