AJAX 请求后使用 JS 打开新标签页被阻止的解决方法

需求:发起一个 AJAX 请求,根据请求结果来打开一个新页面。

问题:AJAX 请求后,使用 window.open() 方法来打开新页面会被浏览器阻止。

解决方法:在 AJAX 请求之前,就使用 const newWindow = window.open('about:blank'); 来打开一个空白页,AJAX 请求后拿到结果,再使用 newWindow.location = 'xxx'; 来让新页面跳转到需要的地址,

例:

// 在线支付
@action onlinePayment = async() => {
if (!this.selectedPrice) {
message.info('请选择时长');
return;
}
this.payWindow = window.open('about:blank'); // 打开一个新页面
try {
let order;
// 余额支付失败转在线支付
if (this.order.orderNo && this.order.selectedPrice === this.selectedPrice) {
order = await banlaceOnlinePay(this.order.orderNo); // ajax 请求
} else {
// 直接在线支付
order = await postOnlinePay({ goods: { goodsId: this.selectedPrice, goodsNum: 1 } }); // ajax 请求
}
this.changePayResult('paying');
if (order) {
this.payWindow.location = `${getPaymentRoot()}/#/payPage/${order.payId}`; // 修改新页面的 url
this.t = setInterval(async () => {
const status = await getOrderStatus(order.orderNo);
if (status) {
this.changePaid();
this.changeShowPayResult(false);
this.order.orderNo = null;
this.order.selectedPrice = null;
}
}, 2000);
} else {
message.error('支付出现问题');
this.payWindow.close(); // 如果 ajax 出问题,则关闭新窗口
}
} catch (e) {
message.error('支付出现问题');
this.payWindow.close();
}
};

转载于:https://www.cnblogs.com/3body/p/8616940.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值