Please enable / Bitte aktiviere JavaScript!
Veuillez activer / Por favor activa el Javascript![ ? ]
Send the form to the server and receiving data back? - bug-code.com Q&A

Send the form to the server and receiving data back?

0 like 0 dislike
12 views
Submit the form to the server:
\r\r\r\r


Get it on the server:
app.post('/add',urlencodedParser,function (req,res) { console.log(req.body); });

How to make sending Ajax'? What could I send from the server the form data back and display them.
asked by | 12 views

2 Answers

0 like 0 dislike
The answer is easily searched by the search engine.
var data = $('#myform').serialize(); $.ajax({ type: "POST", url: "http://localhost:3000/add?", data: data, success: function(msg) { alert(msg); } });


And don't forget to add the server to function.
answered by
0 like 0 dislike
Turned out to make so:
\r\r\r\r\r\r\r\rlet myButton = document.getElementById('formsub');\rmyButton.addEventListener('click',function (ev) {\rlet name = document.forms['myform'].elements['login-name'].value;\rlet pass = document.forms['myform'].elements['login-name'].value;\r\rlet obj = {\rname:name,\rpass:pass\r};\rfetch('/add', {method: 'POST',body:JSON.stringify(obj),headers:{'content-type': 'application/json'}})\r.then(function (response) {\rreturn response.json();\r})\r.then(function (data) {\rlet us = document.createElement('h2');\rus.innerHTML = data.name;\rlet mydiv = document.getElementById('tester');\rmydiv.appendChild(us);\r})\r.catch(alert);\r})\r
answered by
Welcome to Bug-code Q&A, where you can ask questions and receive answers from other members of the community.

24.8k questions

46.2k answers

0 comments

13.3k users

24,788 questions
46,220 answers
0 comments
13,322 users