Wednesday, March 11

Redirect request after a jQuery Ajax call.

1. Adding a custom header to the response:
   
    <script>
        public ActionResult Index(){
          if (!HttpContext.User.Identity.IsAuthenticated)
          {
            HttpContext.Response.AddHeader("REQUIRES_AUTH","1");
          }
          return View()   
        }
    </script>


2. Binding a JavaScript function to the ajaxSuccess event and checking to see if the header exists:
   
    <script>
        jQuery('body').bind('ajaxSuccess',function(event,request,settings){
            if (request.getResponseHeader('REQUIRES_AUTH') === '1'){
               window.location = '/';
            };
        });
    </script>



SOURCE: stackoverflow.com

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete