My current issue involves performing a form POST to a 3rd party payment provider using Angular TypeScript and then redirecting to their hosted payment page. When I submit a standard form via a regular HTML page, the redirection happens automatically. However, when using HttpClient.post in Angular, the redirect does not occur.
I have explored observables and injectors in an attempt to find a solution, but so far nothing has addressed this specific problem. I have come across various Angular version-related problems and solutions during my research, which may have caused me to overlook a simple answer.
I am avoiding using a hidden form with hidden fields because I will need to replicate this process in a mobile (Ionic) app later on. I plan to use their built-in HttpClient along with the standard HttpClient for regular web pages.
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
selector : 'app-paymenttest-page',
templateUrl : 'paymenttest.page.html',
styleUrls : ['paymenttest.page.scss']
})
export class PaymentTestPage implements OnInit {
constructor(private http : HttpClient) {
}
private void makePayment(url: str, formData: FormData): void {
let headers = new HttpHeaders({'Accept' : 'text/html' });
this.http.post(url, formData, {
headers : headers,
responseType : 'text',
})
.subscribe(
(res) => {
console.log('post res');
console.log(res);
window.location.href = res.url;
// How do I simulate a form submit redirect with res.body here?
},
(err) => {
console.log('post err');
console.log(err);
});
}
}
My expectation is to achieve a redirect to the 3rd party hosted page, but instead, I only receive the HTML text response from the 3rd party. This is the same response I get when doing a basic HTML form submit, but the form submit somehow manages the redirect.
-- Edit The response I am receiving is the actual HTML payload from the URL I am posting to and expects to be served from that URL. Therefore, my question is: how does a regular HTML form submit navigate to the POST URL and display the returned content from there?