0

On the front end ejs page, I have a ajaxSubmit handler function like this.

    $('#submitDetails').submit(function() {
    $(this).ajaxSubmit({
      error: function(xhr) {
        alert('Error: ' + xhr.status);
      },
     success: function(response) {

      if (response.responseCode == 1) {
        alert("There is an error. Please try again!");
      } else {
        //do nothing
      }
  }
     });
     };

On the server side, I have a nodejs routing module with get and post methods. For the above submit button click, I am calling a post route. Like below

route.post('/submit', (req, res, next) => {

    if (dataRetrieved == 1 || dataRetrieved == 2) {
            return res.json({"responseCode" : dataRetrieved});
    } else {
        res.render('someOtherPage');
    }

When the application works without any server issue, the else block only executes. As you could see, I want to render some other page inside my else block and do not wish to return res.json to the ajaxSubmit handler. It is not allowing me to do that. As soon as it sees res.render, it goes back to that submitHandler function. Is there a way to prevent this?

SamT
  • 165
  • 1
  • 2
  • 11

1 Answers1

0

I got rid of the ajaxSubmit handler completely.I am capturing the server response within the server code itself. In the front end i am using submitHandler instead of ajaxSubmit to submit my form. Like below

$('#submitButtton').validate({
                submitHandler: function (form, event) {
                    //event.preventDefault();
                    //return false;
                    form.submit();
                }
SamT
  • 165
  • 1
  • 2
  • 11