[Stick to the top] jquery sends Ajax requests and submits them again after one click

Source: Internet
Author: User

Problem description:
Front-end page

<Input type = "button" class = "inputbutton" id = "btnlogin" value = "login"/>


Show. JS:

$("#btnlogin").click(function(){    $.ajax( {    url : "LoginAction",    type:"post",    dataType:"json",    data:"username="+userName,    timeout:20000,    success:function(){}  });});


When you click log on, data will be submitted again. Think twice!
Cause analysis:
After reading the analysis from netizens, some said, "the reason for the two submissions is that after the Ajax request is executed, the submit action is not blocked, the solution is not to use the button with the type of submit, but to use the button with the type of button. "However, I use the button with the type = button. Therefore, the reason is excluded. Some netizens said this was a jquery bug. After testing, similar methods were also used on other pages of the program I wrote. This did not happen, so the reason was also ruled out.
After some exploration, I finally found the cause:
I include (top. jsp) on this page (login. jsp), and show. JS is introduced in both pages:

<script type="text/javascript" src="js/show.js"></script>


This leads to the introduction of the show. js file twice, so the file will be submitted twice. In the top. jsp file

<script type="text/javascript" src="js/show.js"></script>


This phenomenon does not occur after deletion. This is a perfect solution!

This mistake reminds me that you must be careful when writing code later.

 

 

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.