Examples of native ajax call data and ajax call
Due to the prevalence of jQuery, jQuery-encapsulated ajax is widely used, because it solves the browser compatibility problem, which is equivalent to removing a big headache for programmers, however, it is not because native ajax disappears, and I still feel familiar with native ajax. The following is an example code of ajax data calling. It is very simple. For Beginners, refer to it. The Code is as follows:
I. Compatibility with browsers
function xmlHttpR(){ var xmlhttp; if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP") } catch(e) { try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){return null;} } } return xmlhttp; }
The preceding Code returns an object compatible with various browsers.
Ii. instance code
Var ajaxEl = new Object (); // ajaxEl is a custom namespace; ajaxEl. contentLoad = function (url) {// cache is enabled in IE browser. Here, the date field is added to the url to prevent IE from using the cache. Of course, you can also use Math. random () produces a similar effect as getTime; url + = "? Date = "+ new Date (). getTime (); this. req = null; this. url = url; // this callback function is the UPDATE function of the data on the page. this. onload = function () {// domEl is the dom element whose ID is # test; var domEl = document. getElementById ("test"); // in addition to the responseText attribute, you can also use responseXml to obtain a data table; domEl. innerHTML = this. req. responseText;} this. xmlhttp (url);} ajaxEl. contentLoad. prototype = {Xmlhttp: function (url) {if (window. XMLHttpRequest) {this. req = new XMLHttpRequest ();} else {try {this. req = new ActiveXObject ("Msxml2.XMLHTTP")} catch (e) {try {this. req = new ActiveXObject ("Microsoft. XMLHTTP ") ;}catch (e) {return null ;}} if (this. req) {var xmlR = this; this. req. onreadystatechange = function () {if (xmlR. req. readyState = 4) {xmlR. onload. call (xmlR) ;}} this. req. open ("GET", url, true); this. req. send (null) ;}}var xmlE = new ajaxEl. contentLoad ("main. php ");
Iii. Code in php
echo "now! time is:".date("H:i:s a Y");
The above is an introduction to the native ajax call data instance, hoping to help you learn it.
Articles you may be interested in:
- Jquery Ajax learning instance 3 sends a request to WebService and calls the method to return data
- Jquery Ajax learning instance 5 sends a request to WebService and returns an asynchronous call to the generic set data.
- How to Use ajax to call json data in jquery
- A small detail about XML data returned by jquery ajax calling webservice with Parameters
- Simple Example of Jquery Ajax parsing XML data (synchronous and asynchronous calls)
- Use ajax technology to call Sina stock real-time data without refreshing