I'm trying to pass params via XMLHttpRequest and get 'undefined'-
Client :
var xj = new XMLHttpRequest();
var params = JSON.stringify({
PreviousTab: "cnn",
CurrentTab: "bbc"
});
xj.open("GET", "http://localhost:8080/api/traceTabs", true);
xj.setRequestHeader("Content-Type", "application/json");
xj.setRequestHeader ("Accept", "application/json");
xj.send(params);
Server (Node.js):
app.get('/api/traceTabs', function (req, res) {
console.log('change url from ' + req.body.PreviousTab +
' to ' + req.body.CurrentTab); // return 'undefined'
});
server.js Config (Node.js):
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var port = process.env.PORT || 8080;
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app);
app.listen(port);
console.log('Listen to port ' + port);
exports = module.exports = app;
All the options that I've tried to get the params return 'undefined':
req.body.PreviousTab / req.param('PreviousTab') and so on.
Can someone help?
I'm trying to pass params via XMLHttpRequest and get 'undefined'-
Client :
var xj = new XMLHttpRequest();
var params = JSON.stringify({
PreviousTab: "cnn.",
CurrentTab: "bbc."
});
xj.open("GET", "http://localhost:8080/api/traceTabs", true);
xj.setRequestHeader("Content-Type", "application/json");
xj.setRequestHeader ("Accept", "application/json");
xj.send(params);
Server (Node.js):
app.get('/api/traceTabs', function (req, res) {
console.log('change url from ' + req.body.PreviousTab +
' to ' + req.body.CurrentTab); // return 'undefined'
});
server.js Config (Node.js):
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var port = process.env.PORT || 8080;
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app);
app.listen(port);
console.log('Listen to port ' + port);
exports = module.exports = app;
All the options that I've tried to get the params return 'undefined':
req.body.PreviousTab / req.param('PreviousTab') and so on.
Can someone help?
Share Improve this question edited Dec 21, 2017 at 14:08 itays02 asked Dec 21, 2017 at 13:13 itays02itays02 831 silver badge6 bronze badges 3- can you add the middleware related server side code – Stamos Commented Dec 21, 2017 at 13:16
- 1 you need to POST or PUT for params to be sent - check your browsers developer tools networks tab, you'll see no params are sent in the request – Jaromanda X Commented Dec 21, 2017 at 13:20
- see definition of XMLHttpRequest send() -> xhr.spec.whatwg/#the-send()-method - to be more accurate re my previous ment, for GET/HEAD requests, send body is forced to null - so, PUT/POST methods are not the only methods you can send with a body - though, they are the most mon – Jaromanda X Commented Dec 21, 2017 at 13:27
2 Answers
Reset to default 5As mentioned, GET or HEAD requests cannot have a body. If your data is large, you should move to a POST request.
However, if the parameters you want to use are short like the ones in the example, you should use query strings:
var url = "bla.php";
var params = "somevariable=somevalue&anothervariable=anothervalue";
var http = new XMLHttpRequest();
http.open("GET", url+"?"+params, true);
http.send(null);
on the node side, assuming you use express, you can read the variables using:
var somevariable = req.query.somevariable;
var anothervariable = req.query.anothervariable;
From XMLHttlRequest.send()
docs:
... If the request method is GET OR HEAD,
the argument is ignored and the request body is set to null.
Change your sending method to POST.