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)<转>的更多相关文章

  1. MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)【转】

    MVC 默认 Request 方式为Get. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三" ...

  2. MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)

    MVC 默认 Request 方式为 Post. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三&qu ...

  3. 【记录】ASP.NET MVC JsonResult JsonRequestBehavior AllowGet

    JS Ajax 调用代码: $.ajax({ url: "/AjaxController/GetInfoById", type: 'GET', datatype: "js ...

  4. MVC下判断用户登录和授权状态方法

    MVC下判断用户登录和授权状态方法 在我们日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization ...

  5. ASP.NET MVC下使用文件上传

    这里我通过使用uploadify组件来实现异步无刷新多文件上传功能. 1.首先下载组件包uploadify,我这里使用的版本是3.1 2.下载后解压,将组件包拷贝到MVC项目中 3.  根目录下添加新 ...

  6. MVC的JsonResult用法

    在Asp.net Mvc 2中由于对数据的保护,默认情况下request为post,所以在前端请求的时候则需要以post方式request action方法: public JsonResult Ge ...

  7. Knockout官网实例在MVC下的实现-02,实现计次

    本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. 当次数达到3: View视图 页面包含三个部分:1.显示点击按钮的次数2.button按钮 ...

  8. Knockout官网实例在MVC下的实现-01,实现Hello world

    本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. View视图 Knockout的一个特点是:声明式绑定,即Declarative bind ...

  9. ASP.NET MVC之JsonResult(六)

    前言 这一节我们利用上节所讲Unobtrusive Ajax并利用MVC中的JsonResult来返回Json数据. JsonResult 上节我们利用分部视图返回数据并进行填充,当我们发出请求需要获 ...

随机推荐

  1. 如何禁用python警告

    有-W选项. python -W ignore foo.py 所属网站分类: python基础 > 综合&其它 作者:jiem 链接:http://www.pythonheidong.c ...

  2. C语言学习11

    直接插入排序 //直接插入排序 #include <stdio.h> void main() { ], i; int insort(int a[], int n); printf(&quo ...

  3. 【HDU 6006】Engineer Assignment(状压DP)

    Problem Description In Google, there are many experts of different areas. For example, MapReduce exp ...

  4. [SQL]数据库中对值为数字,存储格式为varchar类型的字段进行排序

    如果要对数据库中某存储数字的列(存储类型不为int)进行排序,可以在order by 里对该列进行转换, 即如 order by cast(mycolumn as int) desc

  5. js总结(三):面向对象,prototype ,oo模拟

    http://aralejs.org/class/docs/competitors.html http://javascript.crockford.com/prototypal.html proto ...

  6. C 题 KMP中next[]问题

    题目大意: 找到能够进行字符串匹配的前缀 这题只要一直求next,直到next为0停止,记得答案是总长减去next的长度 #include <iostream> #include < ...

  7. [NOIP2003] 提高组 洛谷P1041 传染病控制

    题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...

  8. [NOIP2002] 提高组P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  9. 时间戳转换成DateTime

    select DateAdd(hour,8,Dateadd(ss,时间戳,'1970-01-01'))   --1970/01/01+时间戳(秒数)+8小时 --因GMT是中央时区,北京在东8区,相差 ...

  10. HDU——2768 Cat vs. Dog

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...