I tried searching around the web but couldn't fix my problem with the answers.
I have two node js servers running on my localhost.
Backend: localhost:9000
Frontend: localhost:3000
I have a jquery Ajax call in my frontend server which looks like this:
$(document).ready(function() {
$.ajax({
url: "http://localhost:9000/merchant",
type: "post",
data: {content: "{"+
'"0": {"name":"kfc", "desc":"great chicken", "web":"www.kfc.com"},'+
'"1": {"name":"pizzaexpress", "desc":"your favourite pizza express", "web":"www.pizzaexpress.com"},'+
'"2": {"name":"indianmasala", "desc":"great indian food", "web":"www.masala.com"},'+
'"3": {"name":"starbucks", "desc":"all coffeee", "web":"www.starks.com"}'+
"}"}
}).done(function(data) {
alert(data);
});
});
Yes, I post a JSON in content parameter. Which is getting stored in my mongoDB and should be returning the inserted ids.
My backend app.post function looks something like this:
var content = JSON.stringify(req.body.content);
content = JSON.parse(req.body.content);
var ids = new Array();
for (var key in content) {
if (content.hasOwnProperty(key)) {
var merchant = new Merchant();
merchant.name = content[key].name;
merchant.desc = content[key].desc;
merchant.web = content[key].web;
merchant.save();
ids[key] = merchant._id;
}
}
var response = {"status":"Request processed successfully", "id": ids};
console.log(JSON.stringify(response));
res.contentType('json');
res.send(JSON.stringify(response));
When I load the frontend localhost:3000 on my browser the AJAX calls the localhost:9000/merchant which then stores the merchant information in mongodb but neither
res.send();
nor
res.write();
send the response. I checked the console.log for the JSON.stringify(response) which looks fine.
My response is empty when I see it in firebug.
Not sure what going wrong. Much appreciate if you lot give me an answer.
Many thanks.