JsonResult作为Action返回值时的错误
JsonResult作为Action返回值时的错误
System.InvalidOperationException: 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.
由错误信息可知MVC2出于对网站数据的保护,默认禁止通过get的请求返回JsonResult数据,你可以在返回Json时,传入第二个参数 JsonRequestBehavior.AllowGet,如:return Json(result, JsonRequestBehavior.AllowGet),当然你也可以修改你的前端代码,使用post的方式来获取数据。
上面是引用网上一位老兄写的,JsonResult返回值里的第二个参数JsonRequestBehavior.AllowGet,有好多朋友都不理解是什么意思,我自己一开始也不明白,后来想了好长时间,才明白,其实很简单的。我们来看一个例子:如下是一个JsonResult和一个Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.MobileControls; namespace MvcApplication1.Controllers
{
public class TestJsonController : Controller
{
//
// GET: /TestJson/ public ActionResult Index()
{
return View();
} public JsonResult Json()
{
List<Address> data = new List<Address> {
new Address{ID=1,Addresses="江苏",ParentID=0},
new Address{ID=2,Addresses="浙江",ParentID=0},
new Address{ID=3,Addresses="福建",ParentID=0},
new Address{ID=4,Addresses="苏州",ParentID=1},
new Address{ID=5,Addresses="常州",ParentID=1},
new Address{ID=6,Addresses="无锡",ParentID=1}
};
return Json(data);
} public class Address
{
public int ID { get; set; }
public string Addresses { get; set; }
public int ParentID { get; set; }
}
}
}
下面的是HTML和JavaScript代码
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title> <script type="text/javascript" src="http://www.cnblogs.com/Scripts/jquery-1.4.1-vsdoc.js"></script> <script type="text/javascript">
$(function() {
// $.getJSON("<%= Url.Action("Json") %>",function(data){
// $("#Address").empty();
// var html="";
// $.each(data, function(entryIndex, entry) {
// if(entry["ParentID"]==0)
// {
// html+=entry["Addresses"]+" ";
// }
// if(entry["ParentID"]==1)
// {
// html+="<br>";
// html+=entry["Addresses"]+" ";
// }
// });
// $("#Address").append(html);
// });
$.ajax({
type: 'POST',
url: '<%= Url.Action("Json") %>',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: '{}',
success: function(data) {
$("#Address").empty();
var html = "";
$.each(data, function(entryIndex, entry) {
if(entry["ParentID"]==0)
{
html+=entry["Addresses"]+" ";
}
if(entry["ParentID"]==1)
{
html+="<br>";
html+=entry["Addresses"]+" ";
}
});
$("#Address").append(html);
alert(data[0].Addresses);
}, error: function(xhr) {
alert('出现错误,服务器返回错误信息: ' + xhr.statusText);
}
}); });
</script> </head>
<body>
<div id="Address">
</div>
</body>
</html>
是一张没有JsonRequestBehavior.AllowGet参数,我用URL直接访问出错图


我在Json的返回值里没有加JsonRequestBehavior.AllowGet,这个参数,但是读取Json也正常,这就是就证了不带这个参数mvc2默认为了安全性考虑,怕直接通过URL去访问JSON,可以下载的,加上了JsonRequestBehavior.AllowGet这个参数就可以用Jquery中getJson()或是Ajax方法都可以用了。
JsonResult作为Action返回值时的错误的更多相关文章
- Controller 中Action 返回值类型 及其 页面跳转的用法
•Controller 中Action 返回值类型 View – 返回 ViewResult,相当于返回一个View 页面. -------------------------------- ...
- 编写JsonResult封装JSON返回值(模板参阅)
编写JsonResult封装JSON返回值 package cn.tedu.note.util; import java.io.Serializable; import cn.tedu.note.se ...
- Java学习笔记14---this作为返回值时返回的是什么
有时会遇到this作为返回值的情况,那么此时返回的到底是什么呢? 返回的是调用this所处方法的那个对象的引用,读起来有点绕口哈,有没有想起小学语文分析句子成份的试题,哈哈. 一点点分析的话,主干是& ...
- linux命令执行返回值(附错误对照表)
转自:http://blog.sina.com.cn/s/blog_6739945f0100zt4b.html 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序在结 ...
- 委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解
1.使用简单委托 namespace 简单委托 { class Program { //委托方法签名 delegate void MyBookDel(int a); //定义委托 static MyB ...
- Block作为返回值时的使用
昨天使用一个小例子简单说明了下Block作为参数时的使用. 今天再来复习一下Block作为返回值使用时的情况,先贴一小段热门第三方框架Masonry的官方sample code: [view1 mas ...
- ajax当有返回值时
当ajax方法里面有return 值时,无法使用两种精简版的只能使用经典版 因为ajax 方法时异步的,正确的方式时使用经典版中async:false 设置为同步 默认为true 是异步 正确代码如 ...
- 1.2Web API 2中的Action返回值
本主题描述 ASP.NET Web API 将返回值转换从一个控制器动作到 HTTP 响应消息. 一个 Web API 控制器动作可以返回下列任一操作 ︰ 1.void 2.IHttpActionRe ...
- asp.net mvc 如何将controller 里一个action 返回值为list<>的值输出到view
在controller中:return View(myRole.ToList());在view文件开头加上:@model IEnumerable<LTXYCallCenter.Models.Ro ...
随机推荐
- java8 lamda快速入门
Lambda语法详解 我们在此抽象一下lambda表达式的一般语法: 1 (Type1 param1, Type2 param2, ..., TypeN paramN) -> { 2 sta ...
- # ios开发 @property 和 Ivar 的区别
ios开发 @property 和 Ivar 的区别 @property 属性其实是对成员变量的一种封装.我们先大概这样理解: @property = Ivar + setter + getter I ...
- 30分钟学会XAML
1.狂妄的WPF 相对传统的Windows图形编程,需要做很多复杂的工作,引用许多不同的API.例如:WinForm(带控件表单).GDI+(2D图形).DirectX API(3D图形)以及流媒体和 ...
- js的OOP继承实现
以下视频截图均来自慕课网javascript深入浅出: 这里Student.prototype之所以使用create方法来创建,而不是直接赋Person.prototype的值,是因为如果使用赋值的话 ...
- CSS知识总结(三)
CSS的常用样式 1.字体样式 1)字体名称(font-family) font-family : <family-name> 设置文字名称,可以使用多个名称,或者使用逗号分隔,浏览器 ...
- C#开发微信门户及应用(8)-微信门户应用管理系统功能介绍
最近对微信接口进行深入的研究,通过把底层接口一步步进行封装后,逐步升级到自动化配置.自动化应答,以及后台处理界面的优化和完善上,力求搭建一个较为完善.适用的微信门户应用管理系统. 微信门户应用管理系统 ...
- MySQL设置服务器方法
grant select,insert,update,delete on *.* to root@"%" Identified by "root";
- 周末惊魂:因struts2 016 017 019漏洞被入侵,修复。
入侵(暴风雨前的宁静) 下午阳光甚好,想趁着安静的周末静下心来写写代码.刚过一个小时,3点左右,客服MM找我,告知客户都在说平台登录不了(我们有专门的客户qq群).看了下数据库连接数,正常.登录阿里云 ...
- java中易遗忘的知识,不定时更新……
如果有人问你: "子类继承父类所有非私有(private)的属性和方法这句话对吗?", 如果你回答对的, 那我只能说too young too simple! 关于代码块和成员变量 ...
- shell编程
最吸引人的莫过于及时看到成果的努力,echo就是最好的初学者反馈函数,因为它的存在你可以及时的打印出结果. 1.分支 if[ ]:then elif ;then else fi 2.简单的循环,循环是 ...