Ajax 如何執行 Response.Redirect

Ajax 直接對服務端的Response.Redirect是不感冒的, 另覓途徑, 具體可行辦法以下:ajax

Web Service 服務端:json

public WXService()
    {
        if (!IsValidTicket)
        {
            string result = "{\"success\": true}";
            Context.Response.Clear();
            Context.Response.ContentType = "application/json; charset=UTF-8";
            Context.Response.Headers.Add("Redirect", "true");
            Context.Response.Headers.Add("RedirectUrl", "Errors.aspx?code=401");
            Context.Response.Flush();
            Context.Response.Write(result);
            throw new Exception("Ticket is logout.");
        }
    }

ajax 客戶端:app

全局定義:
$.ajaxSetup({ complete: function (XMLHttpRequest, textStatus) { var redirect = XMLHttpRequest.getResponseHeader("Redirect"); if (redirect == "true") { window.location.href = XMLHttpRequest.getResponseHeader("RedirectUrl"); } } });

響應結果以下:code

 

相關文章
相關標籤/搜索