简体   繁体   中英

How to redirect to external URL with response data (from post) after http.post

this.http.post<any>('https://api.mysite.com/sources', [..body], [...header])
  .subscribe(async res => {
    const someData = res.data;
    const url =  res.url;

    window.location.href = url  
})

This will redirect to specified url but how can I include the someData when the redirection happened?

You can send the data as queryparams

   this.http.post<any>('https://api.mysite.com/sources', [..body], [...header])
      .subscribe(async res => {
         const someData = res.data;
         const url =  res.url;

         window.location.href = url + `?data1=${yourData1}&data2=${yourData2}`
    })

And receive those data when arrive the other application

    private readRedirectionData() {
       const url = window.location.toString();
       const data1 = this.getUrlParameter(url, 'data1');
       const data2 = this.getUrlParameter(url, 'data2');
    }

    private getUrlParameter(url, name) {
       if (!url) {
          return '';
       }
       if (!name) {
          return '';
       }
       name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
       const regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
       const results = regex.exec(url);
       return results === null ? '' : decodeURIComponent(results[1]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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