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

Source: Internet
Author: User

Get,post and Ajax methods can be used in jquery to deliver data to the server side

Use of Get Method (Customforget.js file):

function Verify () {
1. Get the data for the text box

Get in the way of DOM
Document.getelementbyidx ("UserName");
The way to get through jquery
var jqueryobj = $ ("#userName");
Get the value of a node
var userName = Jqueryobj.val ();

2. Send the text box data to the server-side servlet
$.get ("ajaxserver?name=" + username,null,callback);
}
callback function
function callback (data) {

3. Accept data returned from the server side
alert (data);
4. Display server-side returned data to the page
The node to use to display the result information
var resultobj = $ ("#result");
resultobj.html (data);
}

The above file can be written as follows:
function Verify () {
$.get ("Ajaxserver?name=" +$ ("#userName"). Val (), Null,function callback (data) {$ ("#result"). HTML (data);
}

Use of post Methods (Customforpost.js):

function Verify () {
1. Get the data for the text box

Get in the way of DOM
Document.getelementbyidx ("UserName");
The way to get through jquery
var jqueryobj = $ ("#userName");
Get the value of a node
var userName = Jqueryobj.val ();

2. Send the text box data to the server-side servlet
$.post ("ajaxserver?name=" + username,null,callback)//post is also possible to follow the parameters directly behind the URL
$.post ("Ajaxserver", {name:username,test: "test123"},callback);//Pass multiple arguments separated by commas, and if the property value is a variable, write it directly, such as: UserName, If it is a character, enclose quotes, such as "test123".
}
callback function
function callback (data) {

3. Accept data returned from the server side
alert (data);
4. Display server-side returned data to the page
The node to use to display the result information
var resultobj = $ ("#result");
resultobj.html (data);
}


The above file can be written as follows:
function Verify () {
$.post ("Ajaxserver", {name:$ ("#userName"). Val (), Test: "test123"},function (data) {$ ("#result"). HTML (data)});
}

Summary: in fact, get and post methods are similar, as long as the get and post interchange can be, and the location of the parameters of two places are OK;

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)});

The use of Ajax methods (Customforajaxtext) receives data types that are plain text:

function Verify () {
1. Get the data for the text box
The way to get through jquery
var jqueryobj = $ ("#userName");
Get the value of a node
var userName = Jqueryobj.val ();

2. Send the text box 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) receives data types that are XML data:

function Verify () {
1. Get the data for the text box
The way to get through jquery
var jqueryobj = $ ("#userName");
Get the value of a node
var userName = Jqueryobj.val ();

2. Send the text box data to the server-side servlet
$.ajax ({
Type: "POST",
URL: "Ajaxxmlserver",
Data: "Name=" +username+ "&" + "test=123",

DataType: "xml",
Success:function (data) {
First you need to convert the passed DOM object into a 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.