Home>

Ajax applications are very popular,Everyone knows the advantages of ajax,Not much to sayIts shortcomings are really many,For example, destroying the browser's back function, the interface opened by the same URL is not exactly the sameAnd security aspects,As for so many disadvantages,It can't stop everyone from using Ajax, so let's share javascript to achieve Ajax summary.

Ajax applications are very popular,Everyone knows the advantages of ajax,Not much to sayIts shortcomings are really many,For example, if the browser's back function is destroyed, the interface opened by the same URL is not exactly the same.And security aspects,As for so many disadvantages,Can't stop everyone from using ajax ~~~

Here is an example of the steps to implement ajax:

1. First get the data we need from the form

2. Then create the corresponding URL

3. Set the onreadystatechange function

4. Open the connection

5. Send the request

The above are the implementation steps of ajax.Let ’s briefly summarize it step by step.

The first is about the establishment of xrh objects.

function creatxhr () {
 var xhr=null;
 if (window.xmlhttprequest) {
  xhr=new xmlhttprequest ();
 } else {
  xhr=new activexobject ("microsoft.xhr");
 }
}

A little more complete can be written like this:

function creatxhr () {
 var xhr=null;
 try {
  xhr=new activexobject ("msxml2.xmlhttp");
 } catch (e) {
  try {
   xhr=new activexobject ("microsoft.xmlhttp");
  } catch (e2) {
   xhr=false;
  }
 }
 if (! xmlhttp &&typeof xmlhttprequest!="undefined") {
  xhr=new xmlhttprequest ();
 }
}

The above has been compatible with Internet Explorer and non-IE browsers.

Then it's time to send the ajax request. Here is an example of a form item country:

function callreqest () {
 var country=document.getelementbyid ("country"). value;
 if ((country == null) || (country == ""))
  return;
 var url="example.php?country =" + encodeuricomponent (country);
 xhr.onreadystatechange=update;
 xhr.open ("get", url, true);
 xhr.send (null);
}

The above obtained a country form item. The onreadystatechange function is an event that is triggered when the readystate changes.This can be used to determine if action is required.

The onreadystatechange function is as follows:

function onreadystatechange () {
 if ((xhr.onreadystatechange == 4)&&((xhr.state == 200) || ((xhr.state>200)&&(xhr.state<300)))) {
  document.getelementbyid ("countrycode"). value=xhr.responsetext;
 }
}

When onreadystatechange is equal to 4, it means that the resource has been completely downloaded to the client.Ready to use,Also check the status code,Between 200 and 300, open left and right,To refresh.Here is simply a response text to a form item,Of course, you can also return related data such as xml,Related content can view other related introductions.

Finally, as for triggering ajax, you can add the onchange event in the country of the form, so that the user will trigger ajax when entering the form.

ps:javascript ajax summary

1. Create xhr object

method Description
new activexobject ("microsoft.xmlhttp") Suitable for i5 and ie6 supporting window.activexobject etc.
new xmlhttprequest () Suitable for ie7 +/ff/chrome/safari/opera, etc.

Properties and methods of xhr objects

Property or method Description
readystate Communication status,The value is 0 ~ 4, see later
onreadystatechange This event is fired when the readystate changes
responsetext Text format document returned by the server
respon ** ml XML file returned by the server
status Status code, such as 100, 200, 404, 500, etc.
statustext Text corresponding to status code (ok/not found)
abort () Abort current request
open (method, url) Open a request
send (args) send request
setrequestheader (key, value) Set the request header
getresponseheader (key) Get response header value
getallresponseheaders () Returns all header information as key-value pairs

3, readystate attribute

Code Description
0 Represents an uninitialized state.To create an uninitialized xhr object
1 Represents the connection status.The open method has been called and is ready to send a request
2 Represents the sending status.The send method has been called and no response data has been received
3 Receiving statusHave received the headers of the http response,Receiving response
4 Represents the loaded state,At this point the response content has been completely received

  • Previous beautify form with jqTransform
  • Next Introduction to ASP Basics Part 3 (ASP Scripting Basics)