Ajax synchronous post method, get data from Node server side

1844 views javascript
8

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();
                    console.log(formData);
                    $.ajax({
                          type: 'POST',
                          data: formData,
                          async: false,
                          url: "/tests/ajaxSubmit",
                          success: function () {
                                console.log(msg);
                          }
                     })
                  }
});

There is a post method on the server side:

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

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

0

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?

JD

Please login first before posting an answer.