JsonResult作為Action傳回值時的錯誤

來源:互聯網
上載者:User

標籤:

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傳回值時的錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.