ASP.NET MVC2中返回Json异常的解决办法

在ASP.NET MVC1.0中,我们在前段通常会这样做。

            $.ajax({
                    type: 
"GET",
                    url: 
"/role/SaveRoleResource",
                    data: { roleId: roleId, array: item },
                    datatype: 
'json',
                    success: 
function (result) {
                        alert(result);
                    },
                    error: 
function (result1) {
                        alert(result1);
                    }
           });

或者

 

                 $.getJSON("/role/SaveRoleResource", { roleId: roleId, array: item },
                                            
function (data) {
                                                alert(data);
                                            });

后端我们会做这样的代码:

        public ActionResult SaveRoleResource(int roleId,Array array)
        {
            
int count = RoleResourceDal.Update(roleId, array);

            
return Json(count);
        }

同样的代码如果放到ASP.NET MVC2中,就会报如下异常:

 [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.

解决方法,将后端代码改成如下

 

        public ActionResult SaveRoleResource(int roleId,Array array)
        {
            
int count = RoleResourceDal.Update(roleId, array);

            
return Json(count,JsonRequestBehavior.AllowGet);
        }

看到这里,可能您就会明白,ASP.NET MVC2中JsonRequestBehavior默认是JsonRequestBehavior.DenyGet。

 

 

posted @ 2010-06-25 07:57  深山老林  阅读(2483)  评论(3编辑  收藏  举报