0

Is it possible to submit a form using form.submit() and making the submission go through ajax instead of redirecting the url?

Here's what I have:

var form = document.form

form.onsubmit = function() {
  var params = {
    type: "POST",
    url: form.action
  }
  $.ajax(params).then(function(resp) {
     // handle response
  })
}
form.submit()

However the submission does not hit the callback and instead redirects the browser to the form.action url. How can I avoid the redirection and submit via ajax?

5
  • Why do you need to call form.submit() if you want to handle it manually? Commented Apr 9, 2019 at 19:19
  • 2
    did you try onsubmit="return false" ? on form tag properties Commented Apr 9, 2019 at 19:21
  • Calling native submit yourself will bypass event listeners Commented Apr 9, 2019 at 19:22
  • @Jeto i'm writing an extension and for some reason the site i'm writing it for won't accept it and returns an empty response without hitting form.submit() Commented Apr 9, 2019 at 19:33
  • You want to execute ajax request on form action and at the end submit the form too? Why? Commented Apr 9, 2019 at 20:36

3 Answers 3

1

Just don't call form.submit() and execute your code directly:

var form = document.getElementById('form');

var params = {
  type: 'GET',
  url: form.action
}
$.ajax(params).then(function (resp) {
  console.log('ajax call successful');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="form" action="https://jsonplaceholder.typicode.com/todos/1">
</form>

If you already have a form.onsubmit handler and absolutely need to use it, just call it (looks a bit ugly, but does the job):

var form = document.getElementById('form');

form.onsubmit = function () {
  var params = {
    type: 'GET',
    url: form.action
  }
  $.ajax(params).then(function (resp) {
    console.log('ajax call successful');
  });
};

form.onsubmit();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="form" action="https://jsonplaceholder.typicode.com/todos/1">
</form>

Note: changed the verbs to GET for the purpose of these samples.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to return false at the end of your function.

return: false

Otherwise, it will submit the form to the specified URL in the action.

Comments

0

While calling native submit() will bypass event listeners, since you are already using jQuery you could use jQuery submit listener and prevent it when event is triggered programatically

$(form).on('submit',function(e) {
  e.preventDefault()
  var params = {
    type: "POST",
    data: $(this).serialize(),
    url: this.action
  }
  $.ajax(params).then(function(resp) {
     // handle response
  })

// trigger jQuery submit
}).submit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.