简体   繁体   English

如何在 http.post 之后使用响应数据(来自帖子)重定向到外部 URL

[英]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?这将重定向到指定的 url 但是当重定向发生时如何包含 someData?

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

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

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