javascript - 如何在 http.post 之后使用响应数据(来自帖子)重定向到外部 URL
问题描述
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
})
这将重定向到指定的 url,但是当重定向发生时如何包含 someData?
解决方案
您可以将数据作为查询参数发送
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}`
})
并在到达其他应用程序时接收这些数据
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]);
}
推荐阅读
- twitter-bootstrap - 当导航栏处于移动大小时,Bootstrap 4/5 不同的导航栏品牌 css
- proxy - 带有 DietPi 发行版的覆盆子上 Tor 代理的 Iptables
- php - 使用 YII 框架验证数据库中存储的 JSON 文件中的数据
- javascript - 如何在不使用视频标签的情况下显示用户的网络摄像头视频
- scikit-learn - 当我使用“r2”作为评分时,sklearn cross_val_score() 返回 NaN 值
- flutter - $HOME/.zshrc 权限被拒绝
- reactjs - react js测试用例文件中如何模拟构造函数变量?
- plsql - PL/SQL UTL_FILE:循环如何自动返回下一行?
- java - session.getAttribute("用户名").toString(); JSP 页面出现错误?
- dataframe - 哪个是在 pyspark 中查询和连接配置单元表的最佳方法