简体   繁体   English

从 jquery ajax 状态码 401 获取位置重定向 url

[英]Get location redirect url from jquery ajax statuscode 401

I currently have the below code and need to get the location parameter to redirect to.我目前有以下代码,需要获取要重定向到的location参数。 How do I grab it with the statusCode setup?如何使用 statusCode 设置来获取它?

Ajax Call Ajax 调用

$.ajax({
                url: url,
                data: (def.data ? (def.convertToJson ? JSON.stringify(def.data) : def.data) : ''),
                type: def.type,
                dataType: def.dataType,
                cache: def.cache,
                contentType: def.contentType,
                statusCode: {
 401: function (response) {
                        debugger
                        window.location = GET LOCATION URL FROM RESPONSE
                    }}

Response Parameter响应参数

response.getAllResponseHeaders()

'access-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept\r\naccess-control-allow-origin: *\r\ncache-control: private\r\ncontent-length: 58\r\ncontent-type: text/html\r\ndate: Mon, 06 Dec 2021 16:51:23 GMT\r\nlocation: https://localhost:44360/store?storeorganizationid=24917#/login?returnurl=https%3a%2f%2flocalhost%3a44360%2fstore%2faccount%3fstoreorganizationid%3d24917%26_%3d1638809474810\r\nserver: Microsoft-IIS/10.0\r\nx-aspnet-version: 4.0.30319\r\nx-aspnetmvc-version: 5.2\r\nx-exposure-server: EastUS2\r\nx-exposure-sport: Basketball\r\nx-powered-by: ASP.NET\r\nx-sourcefiles: =?UTF-8?B?RjpcTXkgV2Vic1xCYXNrZXRiYWxsVG91cm5hbWVudHNcTmV3UmVnaXN0cmF0aW9uXFdlYnNpdGVzXFRvdXJuYW1lbnRzXHN0b3JlXGFjY291bnQ=?=\r\nx-ua-compatible: IE=Edge,chrome=1\r\n' 'access-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept\r\naccess-control-allow-origin: *\r\ncache-control: private\r\ncontent-length: 58 \r\n内容类型:文本/html\r\n日期:2021 年 12 月 6 日星期一 16:51:23 GMT\r\n位置:https://localhost:44360/store?storeorganizationid=24917#/login?returnurl= https%3a%2f%2flocalhost%3a44360%2fstore%2faccount%3fstoreorganizationid%3d24917%26_%3d1638809474810\r\n服务器:Microsoft-IIS/10.0\r\nx-aspnet-version: 4.0.30319\r\nx-aspnetmvc- version: 5.2\r\nx-exposure-server: EastUS2\r\nx-exposure-sport: Basketball\r\nx-powered-by: ASP.NET\r\nx-sourcefiles: =?UTF-8?B?RjpcTXkgV2Vic1xCYXNrZXRiYWxsVG91cm5hbWVudHNcTmV3UmVnaXN0cmF0aW9uXFdlYnNpdGVzXFRvdXJuYW1lbnRzXHN0b3JlXGFjY291bnQ= ?=\r\nx-ua 兼容:IE=Edge,chrome=1\r\n'

This will get that value:这将获得该值:

response.split('location:')[1].split("\r\n")[0];

 let response = 'access-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept\r\naccess-control-allow-origin: *\r\ncache-control: private\r\ncontent-length: 58\r\ncontent-type: text/html\r\ndate: Mon, 06 Dec 2021 16:51:23 GMT\r\nlocation: https://localhost:44360/store?storeorganizationid=24917#/login?returnurl=https%3a%2f%2flocalhost%3a44360%2fstore%2faccount%3fstoreorganizationid%3d24917%26_%3d1638809474810\r\nserver: Microsoft-IIS/10.0\r\nx-aspnet-version: 4.0.30319\r\nx-aspnetmvc-version: 5.2\r\nx-exposure-server: EastUS2\r\nx-exposure-sport: Basketball\r\nx-powered-by: ASP.NET\r\nx-sourcefiles: =?UTF-8?B?RjpcTXkgV2Vic1xCYXNrZXRiYWxsVG91cm5hbWVudHNcTmV3UmVnaXN0cmF0aW9uXFdlYnNpdGVzXFRvdXJuYW1lbnRzXHN0b3JlXGFjY291bnQ=?=\r\nx-ua-compatible: IE=Edge,chrome=1\r\n' let loc = response.split('location:')[1].split("\r\n")[0]; console.log(loc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM