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方法都可以用了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM