Angular: Error to POST HTML form (Not ajax) via component after sending 1st Ajax hit? Error:Form submission canceled because the form is not connected
Asked Answered
L

0

0

enter image description hereI want to submit form to external site by POSTing the input fields in old school way(non Ajax) on paypal.

code for answer worked for me. https://mcmap.net/q/1453861/-angular-2-4-how-to-post-html-form-not-ajax-thru-component-on-callback-of-1st-ajax-submit . However what I am stuck with is that: I want to hit 1st ajax on My server. Then I have to post the whole form in old way on paypal on success of 1st ajax.

I used following code in HTML(template)

<form (submit)="onSubmit($event)" method="POST"  [formGroup]="form" *ngIf='form' action="https://www.sandbox.paypal.com/cgi-bin/webscr" >
....
<button class="btn btn-green" [formGroup]="form" type="submit">Pay</button>
</form>

And inside component I used m

onSubmit(obj: any) {

    if (!this.form.valid) {
        this.helper.makeFieldsDirtyAndTouched(this.form);
    } else {
        this.loader = true;
        // save data in online_payment_ipn
        this.paymentService.saveOnlinePaymentIpn({}, 'paypal')
        .subscribe(response => {
            obj.target.submit();
        }, (err: any) => {
            this.loader = false;
            this.helper.redirectToErrorPage(err.status);
        });
    }
}

Thanks in advance!

Now first this form saves data in my site via normal reactive form post(ajax). Now after that I post to 3rd party like paypal whole form in Old way of submitting but I am getting Warning and form is not being submitted to paypal, it just does nothing. If I remove this ajax call and only use

obj.target.submit(); 

It works but I want to hit ajax call on my server 1st.

Form submission canceled because the form is not connected

Any help is appreciated.

Laith answered 24/4, 2018 at 15:53 Comment(6)
The screen that you are using not display the same code written in question, in screen, after subscribe you,re used {} then the error function, then the success function, try to write the same code that you are used in the questionWhitneywhitson
@MohammedElBanyaoui removed image, it was old codeLaith
make sure you have a button element with type="submit"Roye
@FatehMohamed Edited, please check editLaith
try event.preventDefault() inside your submit method onSubmitRoye
tried, no successLaith

© 2022 - 2024 — McMap. All rights reserved.