Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
Sending and proceeding POST request

I have very simple node.js server which has

var express = require('express');
var app = express();

app.post('/savearticles', function (req, res) {
    res.send(req.body);
});

and not much more difficult javascript code

    var xmlHTTP = new XMLHttpRequest();
    xmlHTTP.onreadystatechange = function () {
        if (this.readyState === 4 && this.status === 200) {
            alert(xmlHTTP.responseText);
        }
    }
    xmlHTTP.open('POST', '/savearticles', true);
    xmlHTTP.setRequestHeader('Content-Type', 'application/json');
    xmlHTTP.send('postparameter');

It returns undefined (checked by returning (typeof res.body)). What am I doing wrong?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are missing the body-parser middleware. Install it and your code should work correctly.

const express = require('express')
const bodyParser = require('body-parser')
const app = express()

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

app.post('/savearticles', (req, res) => {
    res.send(req.body)
})
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!