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!