0

In my application forms sometimes is submitted directly, and sometimes by .ajaxForm() from jquery plugin. I want to know how form was submitted. Can I get know about this inside onsubmit event?.

$(document).on('submit', 'form', function(e) {
   if (e.method_to_check_is request_was_ajax)  
   // rest of code here
 });

After few comment I have to edit question.

Piotr Galas
  • 4,518
  • 2
  • 21
  • 30
  • 3
    How would it be Ajax with onsubmit? Ajax is the XMLHttpRequest object, it is not going to trigger the onsubmit of a form. – epascarello Feb 19 '16 at 15:44
  • I don't understand your question, you must know what kind of request you are doing, right? If the form was submited from jQuery (for instance) you must know from were it was done. – rmpt Feb 19 '16 at 15:46
  • http://stackoverflow.com/questions/10783463/javascript-detect-ajax-requests – Niklesh Raut Feb 19 '16 at 15:48
  • http://www.gmarwaha.com/blog/2009/06/27/is-it-an-ajax-request-or-a-normal-request/ – Niklesh Raut Feb 19 '16 at 15:48
  • @epascarello I thought that XMLHttpRequest will trigger on ('submit') event. I will check it – Piotr Galas Feb 19 '16 at 15:51
  • @rmpt in application there are a lot of form submission. Both ajax and not ajax. To all of it i need to add some logic, but I need to know which is ajax and which no – Piotr Galas Feb 19 '16 at 15:53
  • 1
    In the browser, an Ajax call does not trigger a submit handler. – epascarello Feb 19 '16 at 16:05
  • @epascarello you have right, Ajax call does not trigger submit. I was mistake because my application sometimes use ajaxForm() to send ajax from form. So I thought that ajax trigger this event. – Piotr Galas Feb 19 '16 at 17:29

2 Answers2

1

Your event is binded to your submit event, not to an ajax call at this point.

However, if you wanted to implement a check to see what this submit is supposed to do, you could:

Have this in your markup:

<form id="my-id" class="my-forms" method="GET">
 <button type="submit"></button>
</form>

And this in your js:

$('#my-id').on('submit', function(e) {
   e.preventDefault();
   var method = this.method;

   if (method === 'GET') {
   console.log(method);
   // do your ajax.get
   }

   if (method === 'POST') {
   console.log(method);
   // do your ajax.post
} 
});

Of course, this example is contrived as this.method on $('#my-id') would always be 'GET'. However, you could instead change your selector to $('.my-forms') to intercept this event among all elements with the .my-forms class.

kuzyn
  • 1,905
  • 18
  • 30
1

According to your description it is not going to be an ajax request because you are using on('submit', function(){}); which is invoked when you submit html form and it would be an html request to server.

If you still want to check type of if the request is GET or POST, you can check form's method attribute.

$(document).on('submit', 'form', function(e) {
   e.preventDefault();
   var method = this.method;
   if(method=='GET')
   //do something
   if(method=='POST')
   //do something
 });
tmw
  • 1,424
  • 1
  • 15
  • 26