【记录】ASP.NET MVC JsonResult JsonRequestBehavior AllowGet
JS Ajax 调用代码:
$.ajax({
url: "/AjaxController/GetInfoById",
type: 'GET',
datatype: "json",
contentType: "application/json; charset=utf-8",
data: { id: id },
success: function (data) {
if (data) {
alert(data.Id);
alert(data.Title);
}
},
error: function (error) {
$("#result_div").html(error.responseText);
}
});
Controller Action 代码:
[HttpGet]
public async Task<JsonResult> GetInfoById(int id)
{
var result = await service.GetInfoById(id);
return Json(result);
}
注意这种实现在 ASP.NET 5 项目中,是没有任何问题的,但在 ASP.NET MVC 5 中,会报如下错误:
This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
正确代码(AllowGet):
[HttpGet]
public async Task<JsonResult> GetInfoById(int id)
{
var result = await service.GetInfoById(id);
return Json(result, JsonRequestBehavior.AllowGet);
}
参考:
- What 'sensitive information' could be disclosed when setting JsonRequestBehavior to AllowGet
- JSON Problem - JsonRequestBehavior to AllowGet.
- JsonResult作为Action返回值时的错误
【记录】ASP.NET MVC JsonResult JsonRequestBehavior AllowGet的更多相关文章
- Asp.net MVC JsonResult 忽略属性
指定 JavaScriptSerializer 不序列化公共属性或公共字段.无法继承此类. 命名空间: System.Web.Script.Serialization 程序集: System.We ...
- [转]自定义ASP.NET MVC JsonResult序列化结果
本文转自:http://blog.163.com/luckcq@yeah/blog/static/17174770720121293437119/ 最近项目中前台页面使用EasyUI的jQuery插件 ...
- [记录]ASP.NET MVC 2.0 如何使用Html.RadioButtonFor?
在MVC 2.0里支持强类型实体绑定,可以直接使用如 <%: Html.TextBoxFor(model => model.Description, new { @class=" ...
- 在ASP.NET MVC中使用Boostrap实现产品的展示、查询、排序、分页
在产品展示中,通常涉及产品的展示方式.查询.排序.分页,本篇就在ASP.NET MVC下,使用Boostrap来实现. 源码放在了GitHub: https://github.com/darrenji ...
- 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 ...
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
MVC 默认 Request 方式为 Post. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三&qu ...
- asp.net mvc自定义JsonResult类来防止MaxJsonLength超过限制
前不久在做一个项目的时候,我用到了mvc的webapi返回了一个大数据,结果报了500错误,如下图所示: Server Error in ‘/’ Application. Error during s ...
- ASP.NET MVC之JsonResult(六)
前言 这一节我们利用上节所讲Unobtrusive Ajax并利用MVC中的JsonResult来返回Json数据. JsonResult 上节我们利用分部视图返回数据并进行填充,当我们发出请求需要获 ...
随机推荐
- javascript变量的作用域
javascript变量的作用域 基本类型和引用类型 基本类型值指的是简单的数据段,而引用类型值指的是那个可能由多个值组成的对象 讲一个值赋值给变量时,javascript解析器首先要确定是基本类型 ...
- tomcat context配置
<Context path="/ext" docBase="/eqp/export" /> 访问目录 ...
- 用c#创建支持多语言的WinForm应用程序
实现多语言的方法可能有使用资源文件,或者配置xml两种方法吧.没时间研究过多,学习了一下使用资源文件的方法,成功了. 在.net2.0 中,m$ 为我们提供了一种简单方便的方法, 使用资源文件 1.新 ...
- ormlite的使用方法
ormlite是什么? 简单来说,就是我们定义一个实体类,利用这个框架,它可以帮我们吧这个实体映射到我们的数据库中,在Android中是SQLite,数据中的字段就是我们定义实体的成员变量. 官网地址 ...
- windows下C++高精度计时
写代码时,经常会计算某一段代码的运行时间,以下提供一个微秒级别的类供参考 class CTimeCost { public: CTimeCost(const string &str) : m_ ...
- 关于ubuntu16.04中mysql root登陆不了的情况下(大多是未设置密码的情况)
1.先将当前用户改成 root用户:sudo su 2.进入安装路径,我的是:cd /etc/mysql/ 3.打开debian.cnf : gedit debian.cnf 4.找到:user pa ...
- HDU1039-Easier Done Than Said?(字符串处理)
一,题意: 判断三个条件 1:有元音字母 2:不能三个连续元音或辅音 3:不能连续两个相同的字母,除非ee或oo二,思路 写函数一个条件一个条件去判断 #include<iostream> ...
- C#_基础:排序算法
//希尔排序 static int[] ShellSort(int[] array) { if (array != null) { int[] list = { 9, 5, 3, 2, 1 }; fo ...
- Hadoop学习笔记—12.MapReduce中的常见算法
一.MapReduce中有哪些常见算法 (1)经典之王:单词计数 这个是MapReduce的经典案例,经典的不能再经典了! (2)数据去重 "数据去重"主要是为了掌握和利用并行化思 ...
- 使用 SailingEase WinForm 框架构建复合式应用程序(插件式应用程序)
对于一些较小的项目,具备一定经验的开发人员应该能够设计和构建出便于进行维护和扩展的应用程序.但是,随着功能模块数量(以及开发维护这些部件的人员)的不断增加,对项目实施控制的难度开始呈指数级增长. Sa ...