I have an Node.js, Express app that serves as an REST api for Emberjs RESTAdapter. I trying to get this working on Windows Azure. When Ember makes the HTTP request on "/examples/1" as below gives me a 400, Bad Request.
After troubleshooting it seems like the error depends on the request header content type.
Running Jquery ajax calls gives me the following results:
This does not work:
$.ajax({
url: "/users/1",
contentType: "application/json; charset=UTF-8"
}).done(function(data) {
console.log(data);
});
But, when switching to this, it works.
$.ajax({
url: "/users/1",
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
}).done(function(data) {
console.log(data);
});
Now, how do I set up my nodejs/azure solution to allow http requests with content type: "application/json; charset=UTF-8"?
Update
After comments, this is the server side code
var express = require('express');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser());
app.use('/', express.static(__dirname + '/WebContent'));
app.use(express.cookieParser());
});
app.get('/:param/:id', function(req, res) {
res.json(200, {user: {id:1, name: 'tester'}} )
});
app.get('/:param', function(req, res) {
res.json(200, {users: [{id:1, name: 'tester'}]})
});
app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
I know this is an old question, but I think I may have also run across this and found a solution. For me, the problem is the way express treats the empty body as invalid JSON when you set the ContentType: application/json header on a GET request.
The suggested work around is to set the contentType to text/plain if you are not sending data.