Form form submission to Ajax submission

Source: Internet
Author: User
Tags button type

In the process of doing the project encountered in order to submit form form to Ajax submission, the following is how I summarized the form form submission seamlessly into the Ajax method of submission.

The original form table Danchang this:
<form action= "xxx" method= "get" >//The value of the action is the requested URL address<div class= "Form-group" > <label for= "Name" > name </label> <input type= "text" class= "Form-control" name= "name" > </div> <div C lass= "Form-group" > <label for= "Jobnumber" > Work No. </label> <input type= "number" class= "Form-control" name= "Jobnumber" > </div> <div class= "Form-group" > <label for= "Nation" > Nationalities </label> <input type= "text" class= "Form-control" name= "Nation" > </div> <d IV class= "Form-group" > <label for= "Gender" > Gender </label> <input type= "text" class= "Form-control" name= "Gender" > </div>
<div class= "Modal-footer" >
<button type= "button" class= "btn Btn-default" data-dismiss= "modal" >Close</button>
<button type= "Submit" class= "BTN btn-primary" > Submit </button>
</div>
</Form>
To convert to an AJAX submission, you need to make the following changes:

1. Remove the attribute action and method of the form element, add id= "MyForm", and the form element becomes <form id= "MyForm" >

2. Change the Type= "submit" button to Type= "button"

3. Write in JS file

$ (function() {    $.ajax ({    "GET",    "xxx",       function (data) {         console.log (data);    // data is returned in the background     }});

In this way, you can happily convert form form submissions to AJAX requests.

PS: If you want to include a custom HTTP header in an AJAX request, add the

function (Request) {Request.setrequestheader ("Content-type", "Application/json");  
In this way, you can set a custom HTTP header field},

Form form submission to Ajax submission

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.