Problems caused by button in HTML form __html

Source: Internet
Author: User
Tags error handling html form chrome developer

The form contains a button such as

<form>

<button class= "Btn MICRO-BTN" > submitted successfully. </button>

</form>

Then call the Jquery.ajax method when you click the button. This would have been a very simple question, obviously the AJAX call is working properly. But the problem is that Ajax calls always fail. The Fail method was registered in the Ajax callback, as follows:

Fail (function (JQXHR, status) {
Console.dir (JQXHR);
Console.log (status);
});


There is always a way to get into the fail, for no reason. And after fail, the current page is always refreshed. This can not be solved. In the network of Chrome Developer tool, the request was found to be incomplete, not even the request method. Response is not displayed.


Search half a day to find such an article: Http://stackoverflow.com/questions/13892529/ajax-request-fails-cant-see-why. The fail code here refers to:

$.ajax ({/
    * AJAX Options omitted */
    error:function (XMLHttpRequest, Textstatus, Errorthrown) {
         if ( xmlhttprequest.readystate = 0 | | Xmlhttprequest.status = = 0) return 
              ;  It's not really a error
         else
              /do normal error handling
});
"Error with status or ReadyState = 0 can occur when you cancel Ajax request before it completes, or when you navigate to Another page or when you refresh page.

This shows that the underlying AJAX principles are not known. Commissioning a look, sure enough as he said. So what's going on is canceling the AJAX request. The previous mention page refreshes, and this refresh actually cancels the AJAX request. Why refresh, search the "form button" after found some clues, button is a type attribute, reference here, there are three values, Button,reset,submit, and each browser may default value. Chrom seems to default to submit. And this button is in the form, so the default click will submit form.


That would be a complete explanation. So the easiest way to do this is to set the button's Type property to a button. The second approach is like the http://stackoverflow.com/questions/8121958/here. Jquery-ajax-post-inside-a-form-prevent-form-submission-on-ajax-call, add JS method prevernt form Submit, or move button to outside form.


The button was previously working correctly because the button was unconsciously written out of the form. This time the unconscious moved into the form, so there was a problem.

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.