I am not busy with my recent work. I would like to take some time to learn about Ajax and learn from my previous experiences.
ArticleSource: http://www.cnblogs.com/oneword/archive/2011/06/04/2072585.html Preface
The first parameter of the open method of the XMLHTTPRequest object is request-type. The value can be get or post. This article describes GET requests.
The purpose of a GET request is to obtain data. Although a GET request can transmit data, the purpose of data transmission is to tell the server what content is provided to us.
When a GET request is used, parameters are transmitted along with the URL.
GET requests are easy to be cached.
When using GET requests, the server must use request. querystring [data] to obtain data.
Problems Found
We used get requests in the complete Ajax instance, and encountered the following problems:
- How to create an XMLHTTPRequest object that can be run in most browsers.
- Cache problems when using GET requests
- Chinese garbled characters
The first problem has been solved in XMLHTTPRequest object;
The reason for the second problem is:
Each time a GET request accesses the cache, it checks whether there are matching URLs. If yes, it returns the cached URL. If no, it sends a request to the server.
Solution:
1. Add a dynamic parameter to the URL so that different URLs are accessed each time, so that a new request is sent to the server each time.
For the third question, let's first look at the cause of Garbled text:
The data returned by XMLHTTP is a uft-8 by default. If the client page is gb2312 or another encoding, garbled characters are generated.
Solution:
1. If the client is gb2312, specify the output stream encoding during output.
2. Both the client and server use UTF-8 encoding.
3. Use the encodeuricomponent method to encode the parameters.
Note:
Example
The HTML file on the page has not changed, and the changed knowledge JSCodeThe Code is as follows:
< Script Type = "Text/JavaScript"> Function Btn_click (){ // Create an XMLHTTPRequest object VaR XMLHTTP = Window. XMLHttpRequest? New XMLHttpRequest (): New Activexobject ( "Microsoft. XMLHTTP" ); // Obtain the value VaR Username = Document. getelementbyid ( "Txt_username" ). Value; VaR Age = Document. getelementbyid ( "Txt_age" ). Value; // Configure the XMLHTTPRequest object// Use the encodeuricomponent method to encode the obtained parameters. // Add parameters so that different URLs are accessed each time to avoid caching problems. XMLHTTP. Open ("Get" , "Get. aspx? Username =" + Encodeuricomponent (username) + "& Age =" + Encodeuricomponent (AGE) + "& Random =" + Math. Random ()); // Set the callback function XMLHTTP. onreadystatechange = Function (){ If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200) {document. getelementbyid ( "Result" ). Innerhtml = XMLHTTP. responsetext ;}} // Send the request. Because all parameters are in the URL, null is sent here. XMLHTTP. Send ( Null );} </ Script >
server:
protected void page_load ( Object sender, eventargs E) {response. clear (); string username = request. querystring [ "username" ]; string age = request. querystring [ "Age" ]; response. write ( "Name: '" + username + " '
Age: " + age + "
time: '" + datetime . now. tostring () + "'" ); response. end () ;}
run the Program . We can see that Chinese characters are not garbled, now let's use a tool to see what header information is generated during this visit:
let's also take a look at what information we get?
let's look at the information in the cache:
Query string Information
Content transmitted by the server:
Note:
1. Avoid caching. Using Dynamic URL parameters is a good choice.
2. When a GET request is sent using open, the server uses request. querystring to obtain the parameter.
3. We will write another article about Chinese Garbled text.
I am not busy with my recent work. I would like to take some time to learn about Ajax and learn from my previous experiences.
Source: http://www.cnblogs.com/oneword/archive/2011/06/04/2072585.html Preface
The first parameter of the open method of the XMLHTTPRequest object is request-type. The value can be get or post. This article describes GET requests.
The purpose of a GET request is to obtain data. Although a GET request can transmit data, the purpose of data transmission is to tell the server what content is provided to us.
When a GET request is used, parameters are transmitted along with the URL.
GET requests are easy to be cached.
When using GET requests, the server must use request. querystring [data] to obtain data.
Problems Found
We used get requests in the complete Ajax instance, and encountered the following problems:
- How to create an XMLHTTPRequest object that can be run in most browsers.
- Cache problems when using GET requests
- Chinese garbled characters
The first problem has been solved in XMLHTTPRequest object;
The reason for the second problem is:
Each time a GET request accesses the cache, it checks whether there are matching URLs. If yes, it returns the cached URL. If no, it sends a request to the server.
Solution:
1. Add a dynamic parameter to the URL so that different URLs are accessed each time, so that a new request is sent to the server each time.
For the third question, let's first look at the cause of Garbled text:
The data returned by XMLHTTP is a uft-8 by default. If the client page is gb2312 or another encoding, garbled characters are generated.
Solution:
1. If the client is gb2312, specify the output stream encoding during output.
2. Both the client and server use UTF-8 encoding.
3. Use the encodeuricomponent method to encode the parameters.
Note:
Example
The HTML file on the page has not changed. The changed JavaScript code is as follows:
< Script Type = "Text/JavaScript"> Function Btn_click (){// Create an XMLHTTPRequest object VaR XMLHTTP = Window. XMLHttpRequest? New XMLHttpRequest (): New Activexobject ( "Microsoft. XMLHTTP" ); // Obtain the value VaR Username = Document. getelementbyid ( "Txt_username" ). Value; VaR Age = Document. getelementbyid ( "Txt_age" ). Value;// Configure the XMLHTTPRequest object// Use the encodeuricomponent method to encode the obtained parameters. // Add parameters so that different URLs are accessed each time to avoid caching problems. XMLHTTP. Open ( "Get" , "Get. aspx? Username =" + Encodeuricomponent (username) + "& Age =" + Encodeuricomponent (AGE) + "& Random =" + Math. Random ()); // Set the callback function XMLHTTP. onreadystatechange = Function (){ If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200) {document. getelementbyid ("Result" ). Innerhtml = XMLHTTP. responsetext ;}} // Send the request. Because all parameters are in the URL, null is sent here. XMLHTTP. Send ( Null );} </ Script >
server:
protected void page_load ( Object sender, eventargs E) {response. clear (); string username = request. querystring [ "username" ]; string age = request. querystring [ "Age" ]; response. write ( "Name: '" + username + " '
Age: " + age + "
time: '" + datetime . now. tostring () + "'" ); response. end () ;}
when running the program, we can see that Chinese characters are not garbled. Now we use a tool to see what header information is generated during this access:
let's also take a look at what information we get?
let's look at the information in the cache:
Query string Information
Content transmitted by the server:
Note:
1. Avoid caching. Using Dynamic URL parameters is a good choice.
2. When a GET request is sent using open, the server uses request. querystring to obtain the parameter.
3. We will write another article about Chinese Garbled text.