1

I am trying to get the submitting of POST forms to use AJAX. This works, but unfortunately it also appears to be triggering for GET requests. Does anyone know why?

$(document).on('submit', 'form[method=post]', $(this).ajaxForm({...}));

1 Answer 1

1

Are you preventing default on the click action? here is an example that shows that your first and second arguments in the .on() method are functioning correctly. Your third argument should be structured like the snippet so that an anonymous function does some form handling.

$(document).ready(function (argument) {

    $(document).on('submit', 'form[method=post]', function (event) {
		event.preventDefault();
		$('.status').html('post')
	});

	$(document).on('submit', 'form[method=get]', function (event) {
		event.preventDefault();
		$('.status').html('get')
	});

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="status">click button to change this text</div>
<form method="post"><button type="submit">Post</button></form>
<form method="get"><button type="submit">Get</button></form>

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

1 Comment

It turned out that my code that I provided for the post was fine, but I needed to put event.preventDefault() in the handler for the get request.

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.