Because of the popularity of jquery, it is now using jquery encapsulated Ajax, because the browser compatibility problem is solved, which is tantamount to the programmer to remove a heart scourge, but not native Ajax disappeared, and I feel that the original Ajax have a good understanding of the The following is an AJAX data invocation of the example code, very simple, beginners can refer to. The code is as follows:
A. Compatible browser section
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 above can return an object that is compatible with each browser.
two. Instance code
var ajaxel=new Object (); Ajaxel is a custom namespace, ajaxel.contentload=function (URL) {//ie browser, enables caching, where the URL is added to the Date field to prevent IE from using caching, and of course you can use Math.random
() produce and getTime similar effects; url+= "? date=" +new date (). GetTime ();
This.req=null;
This.url=url;
This callback function is the update function on the page of the data, This.onload=function () {//domel is the DOM element with the ID #test; var domel=document.getelementbyid ("Test");
In addition to using the ResponseText property, 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 ("Micro Soft.
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");
Three. Code in PHP
echo "now! Time is: ". Date (" H:i:s a Y ");
The above is about the native Ajax call data examples, I hope to help you learn.