A summary of the use of Get,post and Ajax methods in jquery

Source: Internet
Author: User

  This article is mainly on the jquery Get,post and Ajax methods of the use of a detailed introduction, the need for friends can come to the reference, I hope to help you.

In jquery, you can use the Get,post and Ajax methods to pass data   Get methods to the server (customforget.js files):   function verify () {//1. Gets the data for the text box  //access to//DOCUMENT.GETELEMENTBYIDX ("UserName") via DOM; Get var jqueryobj = $ ("#userName") via jquery; Gets the value of the node var userName = Jqueryobj.val ();  //2. Sends the text box's data to the server-side servlet $.get ("ajaxserver?name=" + username,null,callback); The//callback function functions callback (data) { //3. Accept a return from the server side of the database//alert (information),//4. Displays the returned data from the server to the page//to the node that displays the result information var Resul Tobj = $ ("#result"); resultobj.html (data);   can write the above file as: Function verify () {$.get ("ajaxserver?name=" +$ ("#userName"). Val (), Null,function callback (data) { $ ("#result"). HTML (data);}); Use of the   POST method (customforpost.js):   function verify () {//1. Get the data for the text box  //Get through DOM Document.getelementbyidx ("UserName"); Get var jqueryobj = $ ("#userName") via jquery; Gets the value of the node var userName = Jqueryobj.val ();  //2. Sends the text box's data to the server-side servlet  //$.post ("ajaxserver?name=" + username,null,callback); Post is also possible to follow the parameters directly after the URLFace $.post ("Ajaxserver", {name:username,test: "test123"},callback);//Pass multiple arguments separated by commas, attribute values if it is a variable write directly, such as: UserName, If it is a character, enclose quotes, such as "test123". The//callback function functions callback (data) { //3. Accept a return from the server side of the database//alert (information),//4. Displays the returned data from the server to the page//to the node that displays the result information var Resul Tobj = $ ("#result"); resultobj.html (data);     You can write the above file as: Function verify () {$.post ("Ajaxserver", {name:$ ("#userName"). Val (), Test: "Test123"}, function (data) {$ ("#result"). HTML (data)});   Summary: In fact, the get and post methods are similar, as long as the get and post interchange can be, and the location of parameters in two places;   such as:   $.post ("Ajaxserver", {name:$ ("#userName") . Val (), Test: "test123"},function (data) {$ ("#result"). HTML (data)});   simply change the post directly to get without modifying the position of the parameter, namely:   $.get ("Ajaxserver", {name:$ ("#userName"). Val (), Test: "test123"},function (data) {$ ("#result"). HTML (data)});   Use of Ajax methods (Customforajaxtext) receive data types that are plain text:   function verify () {//1. Get the text box data//Get Var jqueryobj through jquery = $ ("#userName"); Gets the value of the node var userName = Jqueryobj.val ();  //2. Sends the text box's data to the server-side servlet $.ajax ({type: "POST", url: "Ajaxserver", Data: "Name=" +username+ "&" + "test=123", success:function (data) {$ ("#result"). HTML ( data); } }); }   The use of Ajax methods (Customforajaxtext) receive data types that are XML data:   function verify () {//1. Get the data for the text box//get Var via jquery Jqueryobj = $ ("#userName"); Gets the value of the node var userName = Jqueryobj.val ();  //2. Sends the text box's data to the server-side servlet $.ajax ({type: "POST", url: "Ajaxxmlserver", Data: "Name=" +username+ "&" + "test= 123 ",   DataType:" xml ", Success:function (data) {//The first need to convert the passed DOM object to the jquery object var jqueryobj = $ (data);//Get Message node var messagenods = Jqueryobj.children (); Get text content var responsetext = Messagenods.text (); $ ("#result"). HTML (responsetext); } }); }  
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.