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