Angular2 watch for 302 redirect when fetching resource

前端 未结 2 639
无人共我
无人共我 2020-12-04 01:22

I have a requirement to pull a few resources from another domain held by my company. I want to pull secured HTML content with GET requests.

When a user is signed out

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 02:04

    This is my working code for redirecting to ServiceData. Angular2(4) and ASP net Core.

    private post(url: string, data?: any) {
        return this.http.post(url, data, { headers: this.headers })
          .map(response => this.extractData(response, true));
    }
    
    private extractData(res: Response, mapJson = true) {
        let body: any = res;
        // redirect
        if (body.url.search('ReturnUrl') != -1) {
            let url = new URL(body.url);
    
            // get patch redirect simple /account/login
            let pathname = url.pathname;
    
            // get params redirect simple ?ReturnUrl=%2Fapi%2Fitems%2FGetitems
            let search = url.search;
    
            // 1 navigate with params
            this.router.navigate.navigate([pathname], { queryParams: { returnUrl: search } });
    
            // OR ...
    
            // 2 navigate only pathname
            this.router.navigate.navigate([pathname]);
            return {};
        }            
    
        if (mapJson) {
            body = body.json();
        }
        return body || {};
    }
    

提交回复
热议问题