2012-05-14 43 views
7

Tôi có cuộc gọi ajax tới MVC trả về một phần xem trước. Tất cả đều ổn cho đến khi phiên kết thúc hoặc cookie hết hạn. Khi tôi thực hiện cuộc gọi ajax, nó sẽ hiển thị nội dung bên trong một div có nghĩa là để xem một phần. Làm thế nào tôi có thể phát hiện rằng phiên của tôi đã hết hạn trong cuộc gọi ajax và chuyển hướng chính xác đến toàn màn hình/trangMVC 3/Jquery AJAX/Phiên hết hạn/C# - Xử lý thời gian chờ của phiên durng ajax gọi

+0

Tôi đang gặp phải vấn đề tương tự, bạn đã tìm thấy giải pháp cho điều này chưa? – oqx

Trả lời

4

Bạn có thể tạo bộ hẹn giờ trên máy khách bằng javascript sẽ hiển thị hộp thoại cho người dùng khi phiên đã hết thời gian . Bạn chỉ cần đặt giá trị của bộ hẹn giờ cho bất kỳ thời gian phiên nào của bạn. Sau đó, trên ajax yêu cầu, nó sẽ thiết lập lại đếm xuống là tốt.

var g_sessionTimer = null; 
function uiSessionInit() { 
    id = "uiTimeout"; 
    timeout = 3600000 * 24; // 1 day timeout 
    uiSessionSchedulePrompt(id, timeout); 
    $('body').ajaxStart(function() { 
     // reset timer on ajax request 
     uiSessionSchedulePrompt(id, timeout); 
    }); 
} 
function uiSessionSchedulePrompt(id, timeout) { 
    if (g_sessionTimer) 
     clearTimeout(g_sessionTimer); 
    g_sessionTimer = setTimeout(function() { uiSessionExpiring(id); }, timeout); 
} 
function uiSessionExpiring(id) { 
    // create a dialog div and use this to show to the user 
    var dialog = $('<div id="uiTimeout"></div>').text("Your session with has timed out. Please login again."); 
    $('body').append(dialog); 
    $('#uiTimeout').dialog({ 
      autoOpen: true, 
      modal: true, 
      title: 'Expired Session', 
      buttons: { 
       "OK": function(){ 
        $(this).dialog('close'); 
       } 
      }, 
      close: uiSessionDialogClose 
    }); 
} 

function uiSessionDialogClose(){ 
    // take user to sign in location 
    location = 'http://www.mypage.com'; 
} 
5

Tôi muốn giới thiệu đóng gói tất cả các yêu cầu của bạn thành một yếu tố wrapper:

public class JsonResponse<T> 
{ 
    public JsonResponse() 
    { 
    } 

    public JsonResponse(T Data) 
    { 
     this.Data = Data; 
    } 

    public T Data { get; set; } 
    public bool IsValid { get; set; } 
    public string RedirectTo { get; set; } 
} 

mô hình gì bạn muốn gửi đến khách hàng của bạn là trong dữ liệu.

Để nhận được RedirectTo được điền, tôi sử dụng thuộc tính GlobalAuthorize trong Global.Asax và thêm handle for HandleUnauthorizedRequests.

public sealed class GlobalAuthorize : AuthorizeAttribute 
{ 
    protected override void HandleUnauthorizedRequest 
     (AuthorizationContext filterContext) 
    { 
     if (filterContext.HttpContext.Request.IsAjaxRequest()) 
     { 
      filterContext.Result = new JsonResult 
      { 
       Data = new JsonResponse<bool> 
         { 
          IsValid = false, 
          //RedirectTo = FormsAuthentication.LoginUrl 
          RedirectTo = "/" 
         }, 
       JsonRequestBehavior = JsonRequestBehavior.AllowGet 
      }; 
     } 
     else 
     { 
      base.HandleUnauthorizedRequest(filterContext); 
     } 
    } 

Ngoài ra, tôi đã gói gọn tất cả yêu cầu Ajax của tôi vào một chức năng duy nhất mà kiểm tra cho RedirectTo.

function global_getJsonResult(Controller, View, data, successCallback, completeCallback, methodType) 
{ 
    if (IsString(Controller) 
     && IsString(View) 
     && !IsUndefinedOrNull(data)) 
    { 
     var ajaxData; 
     var ajaxType; 

     if (typeof (data) == "string") 
     { 
      ajaxData = data; 
      ajaxType = "application/x-www-form-urlencoded" 
     } 
     else 
     { 
      ajaxData = JSON.stringify(data); 
      ajaxType = "application/json; charset=utf-8"; 
     } 
     var method = 'POST'; 

     if (!IsUndefinedOrNull(methodType)) 
     { 
      method = methodType; 
     } 

     var jqXHR = $.ajax({ 
      url: '/' + Controller + '/' + View, 
      data: ajaxData, 
      type: method, 
      contentType: ajaxType, 
      success: function(jsonResult) 
      { 
       if (!IsUndefinedOrNull(jsonResult) 
        && jsonResult.hasOwnProperty("RedirectTo") 
        && !IsUndefinedOrNull(jsonResult.RedirectTo) 
        && jsonResult.RedirectTo.length > 0) 
       { 
        $.fn.notify('error', 'Login Expired', 'You have been inactive for a prolonged period of time, and have been logged out of the system.'); 
        window.setTimeout(function() { window.location = jsonResult.RedirectTo }, 5000); 
       } 
       else if (IsFunction(successCallback)) 
       { 
        successCallback(jsonResult, Controller + '/' + View); 
       } 
      }, 
      error: function(jqXHR, textStatus, errorThrown) 
      { 
       if (errorThrown != 'abort') 
       { 
        $.fn.notify('error', 'AJAX Connection Error', textStatus + ': ' + errorThrown); 
       } 

      }, 
      complete: function(jqXHR, textStatus) 
      { 
       if (IsFunction(completeCallback)) 
       { 
        completeCallback(jqXHR, textStatus, Controller + '/' + View); 
       } 
      } 
     }); 

     return jqXHR; 
    } 
} 
+0

Tôi đã cố gắng sử dụng theo cách của bạn, HandleUnauthorizedRequest của tôi hoạt động hoàn hảo, nhưng nó không bao giờ xuất hiện trong phương thức global_getJsonResult(), bạn có thể cho tôi một ví dụ về cách bạn đóng gói phương thức jquery của mình không? – Bharat

+0

Không chắc chắn ý bạn là gì. Ví dụ trên có một phương thức đóng gói các yêu cầu ajax và tất cả các yêu cầu sử dụng phương thức đó trong mã JS của tôi. –

Các vấn đề liên quan