If this question has been answered elsewhere, I apologize. I have been stuck on this issue for the past few hours. I have a simple nodejs application that accepts a message posted from the front end, calls an asynchronous service with the message, and returns the response. Below is the code:
('#btn').click(function (e) {
var message = $('#message').val();
$.ajax({
method: "POST",
url: "./api/text",
contentType: "application/json",
data: JSON.stringify({ text: message })
})
.done(function (res) {
debugger;
console.log('Test'); // Debugger and log never hit
$('#msg_response').html(res);
});
});
/////////////////////////////////////////////////////////////////////////
app.post("/api/text", function(request, response) {
var message = request.body.text;
var client = new Client();
client.doSomethingAsync({
data: message
}, function (err, msg) {
// Getting here, but never returning to 'done' above
response.json(JSON.stringify(msg));
response.end();
});
});
The issue is that the response from the "post" request is never being received from the server. I believe it has something to do with the response being sent in the callback function because if I take the response out of the callback, everything works great. Is it possible to send the response back from an asynchronous call?
Thanks for any help.
via ABondi415
No comments:
Post a Comment