Now I have read some knowledge about Ajax. Write it down as a memorandum of knowledge!

Source: Internet
Author: User

My personal understanding is as follows !!!

 

1. Ajax is divided into page (front-end) and background processing (backend, Servlet );

 

2. Page: Create an XMLHTTPRequest object. The supported XMLHttpRequest objects are unavailable in different browsers;

Page: Create the sending function XMLHttpRequest. Open ("get", URL, true );
XMLHttpRequest. onreadystatechange = processresponse;
XMLHttpRequest. Send (null); // XMLHttpRequest. Send ("uname =" + "LC ")

 

In the open function, the first parameter indicates the request method. There are two types: Get and post.

The second parameter is the webpage or servlet to which the request is submitted.

The third parameter: True indicates asynchronous

 

In the send function: indicates the parameter submitted to the servlet. It can be a simple key-Value Pair parameter, or the entire XML document

 

When a request is submitted using the get method, is the parameter followed by URL or login. do? Uname = Lc;

When submitting using the POST method, you can submit complex data, such as XML documents,

However, you need to follow up when submitting a POST request. XMLHttpRequest. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");

 

3. Page: Create a callback function, processresponse (), used to process data returned from the Servlet

If (xmlhttpreq. readystate = 4 ){
If (xmlhttpreq. Status = 200 ){
VaR res = xmlhttpreq. responsetext;
Window. Alert (RES );
} Else {
Window. Alert ("the page you requested has an exception! ");
}
}

 

4. Page: The returned data can be plain text: XMLHttpRequest. responsetext;

It can also be an XML document: XMLHttpRequest. responsexml;

 

5. Page: display the returned data to the HTML/JSP page through document. All. a00.innerhtml ();

You can also directly operate on DOM objects and add nodes to HTML/JSP.

 

6. Background: The foreground uses get + simple parameters. Background Processing Method

String uname = Req. getparameter ("uname ");
Printwriter out = resp. getwriter ();
Out. println ("<response> ");

If ("LC". Equals (uname) & "LC". Equals (password )){
Out. println ("<res> thank you! </RES> ");
} Else {
Out. println ("<res> sorry, login failed! </RES> ");
}

Out. println ("</response> ");
Out. Close ();

Note: The plain text or XML document is returned at the front end. The XML document is used here.

 

7. Background: The foreground uses the post + XML parameter. Background Processing Method

Stringbuilder bxml = new stringbuilder ();
String line = NULL;
Try {
Bufferedreader reader = Req. getreader ();
While (line = reader. Readline ())! = NULL ){
Bxml. append (line );
}
} Catch (exception e ){
E. printstacktrace ();
}

String xml = bxml. tostring ();
Xml = urldecoder. Decode (XML, "utf8"); // garbled during submission. This is a solution
Document xmldoc = NULL;
Try {
Xmldoc = documentbuilderfactory. newinstance (). newdocumentbuilder ()
. Parse (New bytearrayinputstream (XML. getbytes ()));
} Catch (exception e ){
E. printstacktrace ();
}

String name = xmldoc. getelementsbytagname ("name"). Item (0). getfirstchild (). getnodevalue ();

String responsetext = "";
Printwriter out = resp. getwriter ();
If ("LC". Equals (name) & "LC". Equals (psw )){
Responsetext = "Welcome! ";
} Else {
Responsetext = "sorry, login failed! ";
}

// Set the format and Character Set of output information
Resp. setcharacterencoding ("UTF-8 ");
Resp. setheader ("charset", "UTF-8 ");
Resp. setcontenttype ("text/XML; charset = UTF-8 ");
Out. Write (responsetext );
Out. Close ();

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/aaabendan/archive/2010/02/23/5319596.aspx

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.