Click the solution submitted twice for the Ajax request of the button. The ajax request

Source: Internet
Author: User

Click the solution submitted twice for the Ajax request of the button. The ajax request

The button type on the page is submit: <input type = "submit" value = "Create" id = "submit"/>

Ajax requests in JQuery are:

$(function () {$('#submit').click(function () {var createGenreForm = $('#createGenreForm');if (createGenreForm.valid()) {var obj = {Name: $('#Name').val(),Description: $('#Description').val()};var jsonSerialized = JSON.stringify(obj);$.ajax({type: "POST",url: createGenreForm.attr('action'),dataType: "json",contentType: "application/json; charset=utf-8",data: jsonSerialized,success: function (result) {alert(result.Message);},error: function (error) {alert("There was an error posting the data to the server: " + error.responseText);}});}});});

The reason for two submissions is that after the ajax request is executed, the submit action is not blocked, so there are two solutions:

1. Do not use buttons whose type is submit type, but use buttons whose type is button.

2. In the $ ('# submit'). click function, add return false; To block submit.

One note: Why is English entered in the title? The reason is that this article can be queried by overseas users. I often refer to the blogs of foreign users when I look up information on Google, which helps me solve many problems, so I also want them to refer to the content I wrote. Of course, I cannot translate all the text into English, so I try to stick the code completely and rely on the code to speak.

The above is a solution for clicking the button to submit Ajax requests twice at a time. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.