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返回值时的错误的更多相关文章

  1. Controller 中Action 返回值类型 及其 页面跳转的用法

        •Controller 中Action 返回值类型 View – 返回  ViewResult,相当于返回一个View 页面. -------------------------------- ...

  2. 编写JsonResult封装JSON返回值(模板参阅)

    编写JsonResult封装JSON返回值 package cn.tedu.note.util; import java.io.Serializable; import cn.tedu.note.se ...

  3. Java学习笔记14---this作为返回值时返回的是什么

    有时会遇到this作为返回值的情况,那么此时返回的到底是什么呢? 返回的是调用this所处方法的那个对象的引用,读起来有点绕口哈,有没有想起小学语文分析句子成份的试题,哈哈. 一点点分析的话,主干是& ...

  4. linux命令执行返回值(附错误对照表)

    转自:http://blog.sina.com.cn/s/blog_6739945f0100zt4b.html 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序在结 ...

  5. 委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解

    1.使用简单委托 namespace 简单委托 { class Program { //委托方法签名 delegate void MyBookDel(int a); //定义委托 static MyB ...

  6. Block作为返回值时的使用

    昨天使用一个小例子简单说明了下Block作为参数时的使用. 今天再来复习一下Block作为返回值使用时的情况,先贴一小段热门第三方框架Masonry的官方sample code: [view1 mas ...

  7. ajax当有返回值时

    当ajax方法里面有return 值时,无法使用两种精简版的只能使用经典版 因为ajax 方法时异步的,正确的方式时使用经典版中async:false 设置为同步 默认为true  是异步 正确代码如 ...

  8. 1.2Web API 2中的Action返回值

    本主题描述 ASP.NET Web API 将返回值转换从一个控制器动作到 HTTP 响应消息. 一个 Web API 控制器动作可以返回下列任一操作 ︰ 1.void 2.IHttpActionRe ...

  9. asp.net mvc 如何将controller 里一个action 返回值为list<>的值输出到view

    在controller中:return View(myRole.ToList());在view文件开头加上:@model IEnumerable<LTXYCallCenter.Models.Ro ...

随机推荐

  1. c#官方推荐md5通用加密类

    /// <summary> /// MD5加密 /// </summary> /// <param name="input">需要加密的字符串& ...

  2. Cocoa深入学习:NSOperationQueue、NSRunLoop和线程安全 (转)

    目前在 iOS 和 OS X 中有两套先进的同步 API 可供我们使用:NSOperation 和 GCD .其中 GCD 是基于 C 的底层的 API ,而 NSOperation 则是 GCD 实 ...

  3. 微信小程序(微信应用号)开发ide安装解决方法

    这两天整个技术圈都炸锅了,微信小程序(微信应用号)发布内测,首批200家收到邀请,但是没受邀请的同学,也不用担心,下面介绍一下解决方法. 首先需要下载ide,昨天只需要下载0.9版本的编辑器并替换文件 ...

  4. 使用Beautiful Soup编写一个爬虫 系列随笔汇总

    这几篇博文只是为了记录学习Beautiful Soup的过程,不仅方便自己以后查看,也许能帮到同样在学习这个技术的朋友.通过学习Beautiful Soup基础知识 完成了一个简单的爬虫服务:从all ...

  5. passport源码研究

            passport的验证过程主要依赖具体的验证策略来实现的,比较常用的有session策略.local策略和github策略等,验证逻辑都是在这些策略类中定义的.passport模块的定 ...

  6. angular-JS模仿Form表单提交

    直接上示例代码,有不懂的欢迎留言: $http({ url: "http://localhost:10086/yuanxin/Conference/ImportExcelDataForBus ...

  7. python学习笔记(基础二:注释、用户输入、格式化输出)

    注释 单行:# 多行:上下各用3个连续单引号或双引号 3个引号除了多行注释,还可以打印多行 举例: msg = ''' name = "Alex Li" name2 = name ...

  8. 2016 ICPC青岛站---k题 Finding Hotels(K-D树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...

  9. 多线程并发同一个表问题(li)

    现有数据库开发过程中对事务的控制.事务锁.行锁.表锁的发现缺乏必要的方法和手段,通过以下手段可以丰富我们处理开发过程中处理锁问题的方法.For Update和For Update of使用户能够锁定指 ...

  10. 使用Mavne生成可以执行的jar文件

    到目前为之,还没有运行HelloWorld的项目,不要忘了HelloWorld类可是有一个main方法的.使用mvn clean install命令默认生成的jar 包是不能直接运行的.因为带有mai ...