MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
2011-08-19 15:29
781 查看
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 [b] Json(person,JsonRequestBehavior.AllowGet);[/b] } 这样一来我们在前端就可以使用Get方式请求了: $.getJSON("/FriendLink/GetPersonInfo", null, function(data) { $("#friendContent").html(data.Name); }) 参考 |
相关文章推荐
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)【转】
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- 【记录】ASP.NET MVC JsonResult JsonRequestBehavior AllowGet
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- 利用Jquery让返回的各类数据(string、集合(List<>)、类)以Json数据格式返回,为什么要用到result.d (JQuery ajax 返回数据 使用 .d 的问题)
- JSP标签的<jsp:useBean>和<jsp:setProperty><jsp:getProperty>的使用
- 使用List<Object>封装的JsonObject等类的读取方法
- spring mvc <mvc:annotation-driven>配置使用出现故障
- js中获取 <%=request.getContextPath()%>
- 使用struts2的<s:param>等标签向action传值出现中文乱码的解决方法
- 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet
- dynamic_cast<>的使用方法
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
- eclipse Maven配置以及使用方法<转>
- <精华帖>在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等 .
- <配置> Ubuntu gcc 安装 使用方法
- 使用<jsp:include>,不想写死URL,动态生成URL的解决的方法
- Flex Socket安全策略<policy-file-request/>及应对方法