Answer the question
In order to leave comments, you need to log in
How to return a response to an AJAX request in nodejs?
Here in php everything is clear, we just give something to the request through 'echo'
How about in nodejs? res.end() ?
But code like this doesn't work
Client:
$.post('/get_comments', {id: parseInt($(this).parent().attr('data-id')), function(data){
console.log(data);
}});
var ShowComments_Model = require('../models/ShowComments_Model.js');
module.exports = function (req, res, db, next) {
res.json({data: 12});
}
var ShowComments_Controller = require('./controllers/ShowComments_Controller.js');
app.post('/get_comments', function(req,res,next) {
ShowComments_Controller(req, res, db, next);
});
Answer the question
In order to leave comments, you need to log in
First, jQuery has a data function and you can (and even should) shorten the code to $(this).parent().data('id').
Second, the url /get_comments?jsoncallback=? obviously something is wrong, namely the second question mark.
Thirdly, according to the res.end() documentation for Express:
Although there is an optional data parameter, it's better to write res.json({data: 12}); if only because such a record is shorter and more fun.
Based on what is in the question, it’s impossible to say about other errors, so if it doesn’t help, then show the express call code (it’s trite, but suddenly, for example, you call express.get, although the request is post?)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question