Keron Maurice
Keron Maurice

Reputation: 33

form.submit is not a function - Angular 11

I'm having a problem that I've not been able to resolve for a few days now. I'm creating a form dynamically to POST to an external payment URL and I end up with the error in the subject. Here is a snippet of the code:

var form = <HTMLFormElement>document.createElement("newPlanForm");
form.method = "POST";
form.action = "https://somewebsite.comd/somepage";
form.append('Content-Type', 'application/x-www-form-urlencoded');
form.append('Accept', 'application/json');
form.style.display = 'none';

var account_number: HTMLInputElement = document.createElement("input");
account_number.value='xxxxxxxxx';
account_number.name='account_number';
form.appendChild(account_number);

document.body.appendChild(form);
form.submit();

Any suggestions on how to proceed? Thank you in advance.

Upvotes: 0

Views: 458

Answers (1)

Ganesh
Ganesh

Reputation: 6016

We need do some changes like below,

Create element using 'form' but not 'newPlanForm', which is causing the problem.

  var form = document.createElement("form");
   form.method = "POST";
   form.action = "https://somewebsite.comd/somepage";
   form.style.display = 'none';
  
   // form['Content-Type'] =  'application/x-www-form-urlencoded';
   // form['Accept'] = 'application/json';
   // above attributes are not valid


  var account_number = document.createElement("input");
   account_number.value='xxxxxxxxx';
   account_number.name='account_number';
   form.appendChild(account_number);

 document.body.appendChild(form);
 form.submit();                       // will submit the form

Happy Coding.. :)

Upvotes: 1

Related Questions