Original Eco-Ajax

Source: Internet
Author: User
Tags browser cache

1 var xmh=null;2 if (window.xmlhttprequest)3             {4 xmh=new XMLHttpRequest ();5             }6 else{7 xmh =new ActiveXObject ("Microsoft.XMLHTTP");8             }9 Xmh.open ("POST", "/ajaxservlet?uname=" +$ ("[Name=uname]"). Val (), true);Ten xmh.onreadystatechange=function () { Oneif (xmh.readystate==4&&status==200) { A var Date=xmh.responsetext; - alert (date); -                 } the  -             } - Xmh.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); -Xmh.send (NULL);

The method has three parameters the first parameter is a description of the HTTP method, there are two forms of one is "get", the other is "post", the difference between the two is equivalent to when the form commits if you use the Get method, you can see the URL of the value of the submitted, is displayed, unsafe, post is implicit, Not see the value of the commit, relatively safe, the above example we are using a "get" method, the end of this article I will show you the "post" method.

The second parameter is a URL, which is the page to be processed in the background, that is, relative to the current page executing the code (using an absolute path), in this case "sayhello.ashx." Name= "+name+" &i= "+math.random (), has been a friend of the ASP. I believe this must be no stranger, yes, here is equivalent to the QueryString value, you need to pass the value of passing through this method to enter, If there are multiple values, use "&" as the QueryString, followed by a parameter "I" here with the Math.random () to generate random numbers, mainly to solve the browser cache problem, so that no data submitted every time will not be the same, A good solution to the caching problem.

The third parameter is whether it is set to async, where we certainly choose to be asynchronous and set to "true".

The difference between synchronous and asynchronous is as follows:

Sync request: After the request has occurred, wait for the server to finish before continuing with the current code.

Asynchronous request: After the request has occurred, you can continue to execute the current code without waiting for the server to finish executing

Following is the onReadyStateChange property of HttpRequest, which can be understood as the event handle function that is called every time the ReadyState property changes, so an anonymous function is immediately behind it.

In this anonymous function first in the IF condition judgment and the use of the HttpRequest two properties, respectively introduced,

The first one is Httprequest.readystate, remember the third parameter we set in the open () function above, that is, whether it is set to asynchronous, we set the true, that is, the asynchronous request, so the JavaScript code will continue to execute during the request. The state of the request can be judged by the ReadyState property, and when readystate = 4 o'clock, all response data is received, and the property value is defined as follows:

The Send () method takes a parameter, and if it is requested by "get" the side but is set to NULL can also write anything, because "get" the way to pass the value is passed through the URL, if it is "Post" request way, then the passed parameters can be written in Send ().

Original Eco-Ajax

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.