The Post requests stay pending. I wanted to log the JSON body of the incoming request. The request works when using Postman but the AJAX requests dont work
Node Code:
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var hostname = 'localhost';
var port = 3000;
var app = express();
app.use(morgan('dev'));
var qRouter = express.Router();
qRouter.use(bodyParser.json());
qRouter.route('/')
.all(function(req,res,next) {
res.header('Access-Control-Allow-Origin', "*")
res.writeHead(200, { 'Content-Type': 'application/json' });
next();
})
.post(function(req,res){
console.log(req.body);
res.end("lalal");
})
app.use('/play',qRouter);
app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});
AJAX REQUEST
var d = {
"rating": 5,
"comment": "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
"author": "Paul McVites"
}
$.ajax({
url: 'http://localhost:3000/play',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(d)
});