Gonna try to make this as simple as possible. I've tried a slew of things and have been researching this for some time, and I think I'm missing something small.
Using Angular 12.
Sample form:
<form id="my-form" action="https://example.com" action="post">
<input name="fname">
<button (click)="mySubmitFunction()">SUBMIT</button>
</form>
I have within mySubmitFunction() logic like so:
const myForm = document.querySelector('#my-form') as HTMLFormElement;
myForm.submit();
With this method, a form submission occurs, which takes every <input> with a name attribute to populate the form data. You can see the resultant http request in your Developer Tools in Chrome for example in the Network tab.
My Issue with this is that data goes out to a third-party vendor, and a redirect occurs, redirecting to whatever we tell them to (in this case, it is the same page). So the user experience is that not only this form data but EVERYTHING ELSE on the page is cleared.
Most examples I've seen of what I am asking are essentially to not use the action/method/submit approach, but to define a button click method that creates an http request from scratch (which is the very first thing I tried actually). The problem with that, is that I get CORS issues on the http.post request, no matter what environment. Here is basically what I'm doing there:
postContactInfoToVendor(requestBody: string): Observable<any> {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
// Where the requestBody is formatted like 'fname=Bob'
return this.http.post(environment.vendorUrl, requestBody, { headers });
}
So I'm kinda stuck, wondering if the many great minds out there have solved this before and I just can't find it.
I also notice there are a LOT of additional headers sent along with the form's submit() request, but I have no clue where they are coming from - wondering if this will help with the CORS issues. Headers like "Cookie" with "visitor_id"-type entries, and others.
Possible solutions I'm thinking of:
Hosting my contact form within an
iframeon the page. Then on success redirect, which we are able to configure on the vendor's side, can go to a thank you page.Intercepting the form.submit()'s request somehow, cancel the request and send a new one modeled with the exact same headers. I have been unable to intercept these requests at this time though.
As always, thanks for any help guys.
UPDATE: My specific issue is with Pardot form submission. There is a workaround in Angular for this, though I'm not sure how I feel about it.
Piecing together information from Simulate a JSONP response with JavaScript URLs and How to make a simple JSONP asynchronous request in Angular 2?, I was able to import HttpJsonpModule and use that with
http.jsonp(url + urlEncodedString, 'callback');
Then, I configure the success/error URLs to statically-hosted json files that will be returned in the respective responses to be used in the success/error callbacks (to my understanding). I'll be testing that out over the next couple of days but just wanted to give a current update.
Although, generally speaking, I know my <iframe> solution would have worked as well :)
UPDATE: I've finished my solution using not HTMLFormElement.submit(), nor http.post, but an http.jsonp() request, only because JSONP is supported server-side.
I'll write up my own answer here shortly with all information.