First, jQuery to achieve Ajax
$ (function () { $ ("#userName"). blur (function () { //// Ajax request used for functions prototype: $. Get (URL, data, callback) $.get ("Userservlet", {uname:$ ("#userName"). Val ()},function (receivedata) { $ ("#lblMsg"). HTML (receivedata); });});
// Service-side Userservlet Public void DoPost (httpservletrequest request, httpservletresponse response) throws servletexception, IOException { String userName=request.getparameter ("uname"); Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Print ("This is the user name that the client passed over:" +username+ " , available"); // try not to add ln .
Second, Jquery.ajax ([options]) detailed
$ ("button" "POST", // case insensitive, don't forget to add", "" URL: "Userservlet", // Be sure to use quotes to expand data: {username:$ (" #userName "). Val (), password:$ ("#password"). Val ()}, // username=123& password=456 ", Success:function (receivedata) {$ (" #lblMsg "
// server Public void DoPost (httpservletrequest Request, HttpServletResponse response) throws Servletexception, IOException {request.setcharacterencoding (" Utf-8 "); // If the other side is a POST request, you can handle the Chinese garbled String userName =request.getparameter ("UserName" ); String password =request.getparameter ("password" "Text/html;charset=utf-8" "added successfully, the information added is" +username+ ":" +password);}
Parameter description
--Callback function Beforesend, error, Datafilter, success, complete (call this function when the request is completed)
--Data type dataType/* We must ensure that the MIME type reported by the Web server matches the DataType we have chosen.
For example, XML, the server side must declare Text/xml or Application/xml to obtain consistent results. If you specify an HTML type, any inline JavaScript will execute a similar, specified script type before HTML is returned as a string, and the server-side build JavaScript will be executed before the script is returned as a textual data.
If the JSON type is specified, the obtained data is parsed as a JavaScript object, and the constructed object is returned as a result, and for this purpose he first tries to use Json.parse ().
If the obtained data file resides on the remote server (the domain name is different, that is, the data is obtained across domains), you need to use the JSONP type.
With this type, a query string parameter callback= is created? , this parameter is appended to the URL of the request. The server side should precede the JSON data with a callback function name in order to complete a valid JSONP request.
If you want to specify the parameter name of the callback function instead of the default callback, you can set the JSONP parameter of the $.ajax ().
Expected data type returned by the server. If not specified, JQuery is automatically judged intelligently based on the HTTP packet mime information, such as the XML MIME type being recognized as XML. In 1.4, JSON generates a JavaScript object, and script executes it. The data returned by the server is then parsed against this value and passed to the callback function.
Available values:
"XML": Returns an XML document that can be processed with jQuery.
HTML: Returns plain text HTML information, and the included script tag is executed when the DOM is inserted.
"Script": Returns plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. "Note:" On a remote request (not in the same domain), all post requests are converted to get requests. (because the script tag of the DOM will be used to load)
"JSON": Returns the JSON data.
"JSONP": Jsonp format. When calling a function using JSONP form, such as "myurl?callback=?" JQuery is automatically replaced? is the correct function name to execute the callback function.
"Text": Returns a plain text string */
--Data option to send
You can include either a query string, such as key1=value1&key2=value2, or a map, such as {key1: ' value1 ', Key2: ' value2 '}. If the latter form is used, the data re-sender is converted into a query string
--Advanced options
Cache//Whether the default value is True, (default is False when datatype is script and JSONP)
Async//Whether to use the async default value of True, by default, all requests are asynchronous requests
ContentType//default: "Application/x-www-form-urlencoded")
Context/* Set AJAX-related callback function contexts
$.ajax ({url: "test.html", Context:document.body, Success:function () {$ (this). AddClass ("Done"
Java Foundation--ajax (ii)