Examples of native ajax call data and ajax call

Source: Internet
Author: User

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

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.