How to create a non-ajax post request in javascript?

Now I do it this way.

var form=document.createElement('form'); form.setAttribute('method', 'get'); form.setAttribute('action', 'url'); hidden=document.createElement('input'); hidden.setAttribute('type', 'hidden'); hidden.setAttribute('name', 'name'); hidden.setAttribute('value', 'value'); form.appendChild(hidden); form.submit(); 

But I want to save the cost of creating a DOM

Is it possible to send a post request in javascript without a DOM?

+4
source share
2 answers

Yes.

 document.forms[0].submit() 

represents the 1st form on the page. And this is a typical way to submit a non-ajax form. But the term β€œnon ajax” is misleading in your question, so the β€œPOST” willow is a simple http verb. And there is no difference in using its "ajax" -way or "non-ajax". An artificial distinction that could be made is: "application / x-www.formurlencoded" is the preferred browser format or "application / json" as you do it with "ajax".

PS: It is difficult to answer your question. Of course, you could avoid dynamically creating the form element; but you must use the form element anyway to make the non-adhesive submit.

+3
source

I believe that in reality you will not strike a big blow to productivity, doing it the way you do. Big problems with editing the DOM actually add to the document, as it requires re-rendering, etc.

+1
source

Source: https://habr.com/ru/post/1486426/


All Articles