Native Ajax invoke data instance to explain _ajax related

Source: Internet
Author: User

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.

Related Article

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.