Several Methods for submitting forms using ajax in Jquery (get and post)

Source: Internet
Author: User
Ajax technology brings us a good user experience. At the same time, using jquery can simplify development and improve work efficiency. Next, let's share several ajax submission methods in Jquery, for more information, see the post and get methods for ajax submitted forms in jquery. When using the get method, we can directly use the ajax serialized form $ (Form ID) serialize (); now, let's introduce two methods for submitting form data. $ Get method: The get () method is submitted through remote HTTP. The following describes two methods for submitting form data.

$ Get form submission

The get () method loads information through a remote http get request.

Format

$(selector).get(url,data,success(response,status,xhr),dataType)

Send two parameters to the test. php webpage and ignore the returned value:

$.get("test.php", { name: "John", time: "2pm" } );

Display the test. php return value (HTML or XML, depending on the return value ):

$.get("test.php", function(data){ alert("Data Loaded: " + data);});

Ajax serialized form

$.Form.serialize( NameValuePair )

Virtualize a form and set the Form Control name and value.

Parameters

NameValuePair

Required. Set a virtual form control. The parameter format is {name1 = value, name2 = value2 ,......}

Return Value

The serialized string of the virtual form in the format of username = % E5 % 95% 8A % E8 % 94% A1 & password = 123456.

The. serialize () method can operate jQuery objects that have selected individual form elements, such,And. However, selectThe serialization of tags is generally easier: $ ('form'). submit (function () {alert ($ (this). serialize (); returnfalse ;});Output standard query string:A = 1 & B; = 2 & c; = 3 & d; = 4 & e; = 5$ POST form submission$. PostJQuery. post (url, [data], [callback], [type]): Use the POST method for asynchronous requests.Parameters:Url (String): the URL of the request.Data (Map): (optional) data to be sent to the server, expressed in Key/value pairs.Callback (Function): (optional) callback Function when the load is successful (this method is called only when the Response returns success ).$. Post ("momsg. php ", {" tel ": $ (" # username "). val ()}, function (data) {if (data = 0) // 0 successful 1 unsuccessful 2 incorrect Mobile Phone Number Format {//}});Use the ajax post request to change the text of the p element:$ ("Input "). keyup (function () {txt = $ ("input "). val (); $. post ("demo_ajax_gethint.asp", {suggest: txt}, function (result) {$ ("span" ).html (result );});});Instance

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.