惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
罗磊的独立博客
B
Blog
博客园_首页
A
About on SuperTechFans
有赞技术团队
有赞技术团队
V
V2EX
U
Unit 42
I
InfoQ
IT之家
IT之家
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Help Net Security

博客园 - Yu

Illegal characters in path 依赖注入的 Singleton、Scoped、Transient vs2015 编译报错:The project references NuGet package(s) that are missing on this computer... discuz 迁移 uc_server 报错 The timeout period elapsed prior to completion of the operation or the server is not responding. 微信小程序笔记 xmlns 与 targetNamespace 的解释 Ubuntu64 apache2+lvs+Keepalived NPOI CellStyle 设置 mysql --initialize specified but the data directory has files in it select2 多选设置默认值 VS 2015 IDE 不支持 MS SQL 2000 生成 dbml ng 发生 Error: ELOOP: too many symbolic links encountered... jquery call cross-domain webapi owin self-host HtmlAgilityPack 使用 微信 oauth2 两次回调 EF 热加载 Winform/Asp.net vs2015 使用 Eazfuscator.NET 3.3 The requested page cannot be accessed because the related configuration data for the page is invalid.
Ajax 如何执行 Response.Redirect
Yu · 2017-11-17 · via 博客园 - Yu

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"); } } });

响应结果如下: