Home > other >  Get location redirect url from jquery ajax statuscode 401
Get location redirect url from jquery ajax statuscode 401

Time:12-07

I currently have the below code and need to get the location parameter to redirect to. How do I grab it with the statusCode setup?

Ajax Call

$.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://localhost:44360/store/account?storeorganizationid=24917&_=1638809474810\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'

CodePudding user response:

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://localhost:44360/store/account?storeorganizationid=24917&_=1638809474810\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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related