Ajax synchronous post method, get data from Node server side

1844 views javascript

I am working on an application where on window.unload event, I am making a synchronous ajax POST call to the node server side. This is the code:

$(window).on('unload', function() {
              console.log("in Unload");

              if(!submittedFlag) {
                    var formData = $('#testsForm').serializeArray();
                          type: 'POST',
                          data: formData,
                          async: false,
                          url: "/tests/ajaxSubmit",
                          success: function () {

There is a post method on the server side:

router.post('/tests/ajaxSubmit',async (req,res) => {
//my computation here

How do I send the response computed in the POST method to the front end HTML side. Please help me out.

answered question

1 Answer


Use res.json method to send response to client.

router.post('/tests/ajaxSubmit',async (req,res) => {
  //my computation here
  return res.json({
    message: 'Done'

posted this

Have an answer?


Please login first before posting an answer.