Angular 2 - 使用POST在新窗口中打开URL

时间:2018-01-08 09:00:26

标签: javascript angular angular2-routing

我目前有一个组件需要在提交时弹出一个。 API的响应提供了redirectURL和redirectParams作为弹出窗口。

只需使用window.open默认其方法为GET而非POST。只要以这种方式加载,就会导致Not Found页面。

这就是我目前的做法:

protected makeDepositPopup(depositRequest: PaymentRequest) {
    this.depositService.requestDepositPopup(depositRequest)
        .subscribe(
            (depositEvent: RedirectData | SuccessfulDepositPopup) => this.handleDepositPopupEvent(depositEvent),
            (error: EcommError) => this.handleDepositError(error),
            () => this.eventService.depositAttempt.emit()
        );
}

然后通过handleDeposit事件处理事件:

protected handleDepositPopupEvent(depositEvent: RedirectData | SuccessfulDepositPopup) {
    if (depositEvent instanceof RedirectData) {
        this.handleRedirect(depositEvent);
    } else {
        this.handleDepositSuccessPopup(depositEvent, this.redirectParams);
    }
}

一旦成功,它就会尝试打开一个窗口,其中包含API响应提供的相应URL和参数:

protected handleDepositSuccessPopup(depositEvent: SuccessfulDepositPopup, redirectParams: string): void {
    redirectParams = ""; // to remove undefined value
    depositEvent.params.forEach(function (data, index) {
        if(index == 0) {
           redirectParams += "?" + data["key"] + "=" + data["value"];
        } else {
           redirectParams += "&" + data["key"] + "=" + data["value"];
        }
    });

    window.open(depositEvent.url + redirectParams);
    this.router.navigate(
        ["deposit", this.route.snapshot.data["instrumentTypeCode"], "deposit-details", depositEvent.id], {
            relativeTo: this.route.parent.parent
        });
}

如何以depositEvent.url及其附加redirectParams打开新窗口的方式转换为POST方法,以获得成功的网页回复?

0 个答案:

没有答案