MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
来源:互联网 发布:企业协作软件 编辑:程序博客网 时间:2024/04/30 13:19
MVC 默认 Request 方式为 Post。
action
public JsonResult GetPersonInfo()
{
var person = new
{
Name = "张三",
Age = 22,
Sex = "男"
};
return Json(person);
}
或者
public JsonResult GetPersonInfo()
{
return Json (new{Name = "张三",Age = 22,Sex = "男"});
}
view
$.ajax({
url: "/FriendLink/GetPersonInfo",
type: "POST",
dataType: "json",
data: { },
success: function(data) {
$("#friendContent").html(data.Name);
}
})
POST 请求没问题,GET 方式请求出错:
解决方法
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方式请求了:
$.getJSON("/FriendLink/GetPersonInfo", null, function(data) {
$("#friendContent").html(data.Name);
})
参考
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)
- MVC JsonResult的实例
- MVC JsonResult的用法
- ASP.NET MVC的JsonResult
- MVC应用程序JsonResult()的练习
- MVC - JsonResult
- MVC JsonResult
- 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet
- mvc 中JsonResult
- 在ASP.NET MVC中json的应用(即JsonResult)(asp.net mvc 后台返回json数据方式 )
- MVC中JsonResult输出Json
- ASP.NET编程MVC--JsonResult
- JsonResult的使用
- jsonResult
- 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- Linux下通过lib导出类
- 代理中操作SQL数据库(增、删、改)
- 常用的正则表达式
- 一个开放平台架构的思考
- 如何做好性能测试
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
- adb 常用命令
- Java串口通信详解
- Dom4j下载及使用Dom4j读写XML简介
- asp.net mvc3 配置自定义错误页
- 浅谈BMP图像文件的内部结构(长期研究的总结)
- poj 1887 Testing the CATCHER dp 最大降序
- 堆积排序
- 题目分类