MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)
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 默认 Request 方式为Get. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三" ...
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
MVC 默认 Request 方式为 Post. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三&qu ...
- 【记录】ASP.NET MVC JsonResult JsonRequestBehavior AllowGet
JS Ajax 调用代码: $.ajax({ url: "/AjaxController/GetInfoById", type: 'GET', datatype: "js ...
- MVC下判断用户登录和授权状态方法
MVC下判断用户登录和授权状态方法 在我们日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization ...
- ASP.NET MVC下使用文件上传
这里我通过使用uploadify组件来实现异步无刷新多文件上传功能. 1.首先下载组件包uploadify,我这里使用的版本是3.1 2.下载后解压,将组件包拷贝到MVC项目中 3. 根目录下添加新 ...
- MVC的JsonResult用法
在Asp.net Mvc 2中由于对数据的保护,默认情况下request为post,所以在前端请求的时候则需要以post方式request action方法: public JsonResult Ge ...
- Knockout官网实例在MVC下的实现-02,实现计次
本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. 当次数达到3: View视图 页面包含三个部分:1.显示点击按钮的次数2.button按钮 ...
- Knockout官网实例在MVC下的实现-01,实现Hello world
本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. View视图 Knockout的一个特点是:声明式绑定,即Declarative bind ...
- ASP.NET MVC之JsonResult(六)
前言 这一节我们利用上节所讲Unobtrusive Ajax并利用MVC中的JsonResult来返回Json数据. JsonResult 上节我们利用分部视图返回数据并进行填充,当我们发出请求需要获 ...
随机推荐
- textarea 固定大小,滚动条,限制拖动,文字对齐
取值:$("#ID").val(); 控制大小:加width,height限制(style="width:100px;height:200px;");或row, ...
- Android超精准计步器开发-Dylan计步(申明:来源于网路)
Android超精准计步器开发-Dylan计步(申明:来源于网路) 拿来借鉴学习,向原创者... 地址:http://blog.csdn.net/linglongxin24/article/detai ...
- windous----操作系统基础
操作系统基础 服务软件,控制硬件. 一:什么事操作系统 操作系统就是一个协调,管理和控制和计算机硬件资源控制程序. 用户态:运行应用程序,不可以操作硬件(可以获取cpu的指令集的一个子集,该子集不包 ...
- Python2.7编译失败 Failed to build these modules:_curses_panel _hashlib _ssl
1:*** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.0.0: cannot open sha ...
- 浅析vue的双向数据绑定
vue 的双向数据绑定是基于es5的 object对象的defineProperty属性,重写data的set和get函数来实现的.1.defineProperty 数据展示 Object.defin ...
- iOS FMDB的是使用和注意事项
1.FMDB 默认的使用方法不是线程安全的. 2.Sqlite 默认不支持外键. 3.Sqlite 不支持用 ALTER 关键字给已有表添加外键约束 解决: 1.FMDBDatabaseQueue 2 ...
- ajax post get
1.Ajax post 方法 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&qu ...
- invariant theory 不变量理论
https://baike.baidu.com/item/不变量理论/9224903?fr=aladdininvariant theory 一组几何元素由 k个参数组成的向量 P1表示.若 T为某一变 ...
- cocos2dx 常用的构建工具
理编辑工具Physics Editing ToolsMekanimo 网址:http://www.mekanimo.net/PhysicsBench 网址:http://www.cocos2d-iph ...
- js 进制之间的转换
//十进制转其他 var x=110; alert(x); alert(x.toString(8)); alert(x.toString(32)); alert(x.toString(16)); // ...