Ajax 直接对服务端的Response.Redirect是不感冒的, 另觅途径, 具体可行办法如下:
Web Service 服务端:
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 客户端:
全局定义:
$.ajaxSetup({ complete: function (XMLHttpRequest, textStatus) { var redirect = XMLHttpRequest.getResponseHeader("Redirect"); if (redirect == "true") { window.location.href = XMLHttpRequest.getResponseHeader("RedirectUrl"); } } });
响应结果如下: