Ajax submission form

Source: Internet
Author: User

Ajax forms are often used in projects. Ajax is used in front-end, whether for simple HTML, JSP, or easyui frameworks, the extjs framework actually uses Ajax but encapsulates it, which is more fixed when we use it.

In general, there are two types of Ajax submission forms. One is that no result is returned, that is, the form data is submitted to the background, and the background processing is complete; the other method is to return the result. The information about successful or failed execution in the background must be returned to the foreground.

1. No returned results

The simplest is $ ("# formid"). Submit (); directly submit the form to the background.

2. Items with returned results

This method is the most widely used one, because the execution of the program is successful or not, you need to prompt the user, the program is generally completed in multiple steps, after the completion of the insert operation, you need to initiate a process, in this case, you need to determine the success or failure on the interface.

Ajax itself is a class with returned results. The success method is used to process the returned results in the background.

There are two methods to return results when submitting Ajax forms:

1) serialize form data

  $.ajax({type: "POST",url:your-url,data:$('#yourformid').serialize(),async: false,error: function(request) {alert("Connection error");},success: function(data) {//接收后台返回的结果}  });
Note that the items in the form must have the name attribute. The key-value pairs obtained in the background are key = Name and value = values, note that no matter whether it is an input tag, span tag, or other tag, the name attribute must be available, and the item cannot be obtained in the background without the name attribute.


2) Search for form submission in the window

  // 提交表单  var obj = document.getElementById("xx_iframe").contentWindow;  obj.$("#yourform").form("submit",{success :function(data){//对结果处理}  });
This submission method is used when I use the easyui dialog box to insert information, because a dialog box is displayed on the current interface, and a form is submitted in the button trigger dialog box on the dialog box, the dialog box is another HTML page linked to, so $ ("# formid") cannot find the form in the dialog box. Therefore, you can only submit the form in this way.

In addition, get and post requests encapsulated in Ajax also belong to the class with returned results.

These are the examples we have encountered, and we will try to add new forms later ..

Ajax submission form

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.