Data transfer from client to server?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
33 views
How to do an ajax request?There is an html file with a button. There is a handler for pressing the button which needs to send data to the server. How to register url?
function but() { let a = 3; let xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8080/"); xhr.send(a); document.write(res.text) }

The server receives data:
app.post('/', function (req, res) { let a = req.name; console.log(a); });

But it displays undefined. What am I doing wrong?
by | 33 views

1 Answer

0 like 0 dislike
Need bodyParser
// client function but() { var xhr = new XMLHttpRequest(); xhr.open( 'POST', '/ajax' ); xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' ); xhr.send( 'field=value' ); } // server app.use( bodyParser.json(), bodyParser.urlencoded({ extended: true }) ); app.post( '/ajax', function (req, res) { console.log( req.body ); });
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Apr 20, 2019 by webninja_surgut
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
110,608 questions
257,187 answers
0 comments
40,796 users