I want my node.js server to send different content based on request's content type, my server code is like this
if(req.accepts('text/html')){
console.log("accepts('text/html')");
locals.page = 'posts';
res.render('question.ejs', locals.questions);
}
else if(req.accepts('application/json')){
console.log("'application/json'");
res.json(200,locals.questions);
}
There is a similar post but only for android client in node.js express, how to tell the HTTP request is application/json or html on the content negoition
This is my Jquery code
$.ajax({ type: "GET", url: "/course/abc/questions", contentType:"application/json; charset=utf-8", dataType:"json", success: function(data){ console.log(data.length,data); } });
For some reason, even I set the content type on appication/json, the node.js server side still accepts html rather than json.
A little strange