Friday, 22 January 2021

Angular create invisble form to do form submit method="post" action="url"

https://stackoverflow.com/questions/55122374/how-can-i-create-a-form-dynamically-and-submit-it-in-angular-7

Update:

I have tried several things to send post request with redirect in Angular. But the only one which worked for me was creating hidden <form>

For this I have created this method

submitForm(action, method, formGroup: FormGroup) {
    const form = document.createElement('form');
    form.style.display = 'none';
    form.method = method;
    form.action = action;
    let input;
    for (const [key, value] of Object.entries(formGroup.value)) {
      input = document.createElement('input');
      input.name = key;
      input.id = key;
      input.value = value;
      form.appendChild(input);
    }
    document.body.appendChild(form);
      form.submit();
  }

Update:
Form needs to be appended to body for it to work , or else form submission is not allowed
document.body.appendChild(form);

https://stackoverflow.com/questions/42053775/getting-error-form-submission-canceled-because-the-form-is-not-connected

No comments:

Post a Comment