The first parameter of the open method of the xmlHttPRequest object is request-type, and the value can be get or post. This article introduces the get request.
The purpose of get request is mainly to obtain data. Although get requests can pass data, the purpose of passing data is to tell the server what content to give us.
When using get request, the parameters are passed with the url.
When using get requests, they are easily cached, so you need to pay attention to caching issues.
When using get request, the server side should use Request.QueryString[data] to get data.
We used the get request in the article "Complete Ajax Instance". We encountered the following problems at that time:
- How to create an xmlhttpRequest object that can run in most browsers.
- There is a caching problem when using get request
- Chinese garbled problem
Among them, the first problem has been solved in the article "XMLHttpRequest Object";
For the second problem, the reason is:
The get request will access the cache every time to see if there is a matching url. If there is, the url in the cache will be returned. If there is no, a request will be made to the server.
Solution:
1. Add a dynamic change parameter to the url to find that each time you access a different url, so that a new request will be issued to the server every time.
For the third question, let’s first look at the reasons for the garbled code:
The data returned by xmlHttp is uft-8 by default. If the client page is gb2312 or other encoding, garbled code will occur.
Solution:
1. If the client is gb2312, then when output, specify the output stream encoding.
2. Both the client and the server use utf-8 encoding
3. Be sure to use the encodeURIComponent method to encode the parameters
Pay attention to the example:
The page HTML file has not changed. The knowledge of the change js code is as follows:
<script type="text/javascript"> function btn_click() { //Create XMLHttpRequest object var xmlHttp = window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); //Get the value var username = document.getElementById("txt_username").value; var age = document.getElementById("txt_age").value; //Configure XMLHttpRequest object //Use encode URIComponent method to encode the obtained parameters//Add parameters to access different urls each time to avoid cache 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 the parameters are all in the URL, so null is sent here xmlHttp.send(null); }</script>
Server side:
protected void Page_Load(object sender, EventArgs e){ Response.Clear(); string username = Request.QueryString["username"]; string age = Request.QueryString["age"]; Response.Write("Name:'" + username + "'<br/>Age:" + age + "<br/>Time:'" + DateTime.Now.ToString() + "'"); Response.End();}//Source from http://www.VEVb.com/oneWord/archive/2011/06/04/2072585.html