日期:2014-05-18  浏览次数:20529 次

asp.net mvc3 ajax请求
我在前台发送一个ajax请求时
C# code

 [AuthorizationFilter]
  public ActionResult GetListWithPager(GridPager pager)


AuthorizationFilter实现如下
C# code

 public class AuthorizationFilter : ActionFilterAttribute
    {
        

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                if (filterContext.HttpContext.Session["User"] == null)
                {
                    filterContext.Result = new RedirectResult("/user/user/login");
                    我想实现如果用户没有登录 转到登录 页页
                    这里的代码应该怎么写。。
                    Ajax请求无法实现转向


                }
            }
        }
    }



请问ajax请求时,,没有验证通过 。。怎么在后台实现页面转向。。转到登录页面

------解决方案--------------------
坐等高人指点
------解决方案--------------------
不能这样。

你应该这样,如果用户没有,就返回代码0,前段判断=0后再跳转。
------解决方案--------------------
在过滤器中统一设定返回值为UnAuthority,写一个js的重定向方法Redirect
然后在Ajax请求的OnSuccess方法中判断如果返回值为UnAuthority,调用js重定向方法
function Redirect()
{
document.href.location='/Login';
}
------解决方案--------------------
我是这样做的
if (filterContext.HttpContext.Request.Headers[ "X-Requested-With "] != null)
{
filterContext.Result = new JsonResult { Data = new JsonClass { Message = "您没有权限 ", Success = false } };
}
else
{
filterContext.Result = new RedirectResult( "/Out/LogOut ");
}
前台的AJAX都是调用我写的一个JS方法,这个方法返回的都是JSON,
json: function (url, value, fun, err) {
//封装的Ajax操作
$.ajax({
type: 'POST ',
url: url,
processData: false,
dataType: 'json ',
data: value,
success: function (data) {
if (data.Success == false) {
top.location.replace( "/Oa/Me?msg= ");
// top.location.href = "/Oa/Me?msg= ";
} else {
fun(data);
}
},
error: function () {
if (err)
err();
else {
$.window.loagingClose();
$.dialog.alert( "请求的操作出错,可能原因: <br> 1.网络不稳定,2.系统错误,3.没有相应的权限。 <br> 请检查后重试! ");
}
}
});
}
能实现超时跳转,如果你找到了更好的方法,也请分享下
------解决方案--------------------
我们才开始学哦...
------解决方案--------------------
在文档流里面写入<script>xxxx()</script>调用页面的js执行跳转就可以了

xxxx()是你的js函数 要指定跳转的话就传递一个参数就可以了撒
------解决方案--------------------
这样要看AJAX返回的内容来决定是否要跳转。