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/2072593.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 POST requests.
When using post, the browser sends the field elements and data in each form to the Web server as the entity content of the HTTP message.
When using post, you must set Content-Type to application/X-WWW-form-urlencoded to ensure that the server knows that there are parameter variables in the object.
When post is used, parameters are sent along with the send method, such as send (data ).
Request. Form [data] is required when the server obtains parameters in post mode.
Example
Client htmlCodeUnchanged. The JS Code is as follows:
Function Btn_click (){// Obtain parameters VaR Username = Document. getelementbyid ( "Txt_username" ). Value; VaR Age = Document. getelementbyid ( "Txt_age" ). Value; // Set string parameters and encode them VaR ARGs = "Username =" + Encodeuricomponent (username) + "& Age =" + Encodeuricomponent (AGE ); // Create an XMLHTTPRequest object VaR XMLHTTP = Window. XMLHttpRequest?New XMLHttpRequest (): New Activexobject ( "Microsoft. XMLHTTP" ); // Configure the connection and method // do not worry about caching when using post XMLHTTP. Open ( "Post" , "Post. aspx" , True ); // Set the Content-Type to aApplication/X-WWW-form-urlencoded to inform the server that the object contains parameters. XMLHTTP. setRequestHeader ( "Content-Type" , "Application/X-WWW-form-urlencoded" );// Set the callback function XMLHTTP. onreadystatechange = Function (){ If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200) {document. getelementbyid ( "Result" ). Innerhtml = XMLHTTP. responsetext ;}} // Sending Parameters XMLHTTP. Send (ARGs );}
server code:
protected void page_load ( Object sender, eventargs E) {response. clear (); // use request. form to obtain data string username = request. form [ "username" ]; int age = int . parse (request. form [ "Age" ]); response. write ( "Name: '" + username + " '
Age: " + age + "
time: '" + datetime . now. tostring () + "'" ); response. end () ;}
Now, we still enter the three and 30 pieces of data, and then the utility checks to see what was actually sent.
Overview:
HTTP header sent:
HTTP Header
Cache: (we can see that the POST request will not be changed to the cache)
Post content: (in a get request, data is sent through a URL)
Summary:
This article describes how to use post. In the subsequent articles, we will compare GET requests with post requests. When is it suitable for get requests and post requests.
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/2072593.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 POST requests.
When using post, the browser sends the field elements and data in each form to the Web server as the entity content of the HTTP message.
When using post, you must set Content-Type to application/X-WWW-form-urlencoded to ensure that the server knows that there are parameter variables in the object.
When post is used, parameters are sent along with the send method, such as send (data ).
Request. Form [data] is required when the server obtains parameters in post mode.
Example
The client's HTML code remains unchanged. The JS Code is as follows:
Function Btn_click (){ // Obtain parameters VaR Username = Document. getelementbyid ( "Txt_username" ). Value; VaR Age = Document. getelementbyid ( "Txt_age" ). Value;// Set string parameters and encode them VaR ARGs = "Username =" + Encodeuricomponent (username) + "& Age =" + Encodeuricomponent (AGE ); // Create an XMLHTTPRequest object VaR XMLHTTP = Window. XMLHttpRequest? New XMLHttpRequest (): New Activexobject ( "Microsoft. XMLHTTP" ); // Configure the connection and method // do not worry about caching when using post XMLHTTP. Open ( "Post" ,"Post. aspx" , True ); // Set the Content-Type to aApplication/X-WWW-form-urlencoded to inform the server that the object contains parameters. XMLHTTP. setRequestHeader ( "Content-Type" , "Application/X-WWW-form-urlencoded" ); // Set the callback function XMLHTTP. onreadystatechange = Function (){ If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200) {document. getelementbyid ( "Result" ). Innerhtml = XMLHTTP. responsetext ;}}// Sending Parameters XMLHTTP. Send (ARGs );}
Server code:
Protected voidPage_load (ObjectSender,EventargsE) {response. Clear ();// Use request. Form to obtain dataStringUsername = request. Form ["Username"];IntAge =Int. Parse (request. Form ["Age"]); Response. Write ("Name :'"+ Username +"'<Br/> Age :"+ Age +"<Br/> time :'"+Datetime. Now. tostring () +"'"); Response. End ();}
Now, we still enter the three and 30 pieces of data, and then the utility checks to see what was actually sent.
Overview:
HTTP header sent:
HTTP Header
Cache: (we can see that the POST request will not be changed to the cache)
Post content: (in a get request, data is sent through a URL)
Summary:
This article describes how to use post. In the subsequent articles, we will compare GET requests with post requests. When is it suitable for get requests and post requests.