I am retrieving an image which is stored as in binary form from mongoDB. and sending it as a json to jquery ajax. but console.log from node.js show it is fine but when request comes back to jquery ajax nothing happens, cannot see an image. And also image is saved in DB with type BUFFER. below is the code
jquery ajax code
$.ajax({
url: config.ipaddress+'/getCompanyImage',
contentType: 'image/png',
type: 'GET',
success: function(data, textStatus, jqXHR) {
$.each(data, function(index, result) {
alert('image result ' + result);
$("#headerCompanyImage").attr("src", result);
});
},
error: function (jqXHR, textStatus, errorThrown)
{
}
});
node.js code
exports.getCompanyImage = function(req,res) {
var_company.find({_id : req.session.company},function(err,company){
company.forEach(function(companyLoop){
console.log(companyLoop.company_image.contentType);
console.log(companyLoop.company_image.data);
res.contentType(companyLoop.company_image.contentType);
res.json(companyLoop.company_image.data);
})
});
}