Home > Web Front-end > JS Tutorial > body text

Simple analysis of Ajax data requests_jquery

WBOY
Release: 2016-05-16 18:08:37
Original
1191 people have browsed it

For example:

Copy code The code is as follows:

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 ;

In this way, you can basically create a cross-browser object;
The following is a simple application of ajax, using the XmlHttpRequest object;
Copy code The code is as follows:

var ajaxEl=new Object();
//ajaxEl is a custom namespace;
ajaxEl.contentLoad=function(url){
//Under the IE browser, caching will be enabled. The date field is added to the URL here to prevent IE from using the cache. Of course, you can also use Math.random() to generate a message similar to getTime. Effect;
url ="?date=" new Date().getTime();
this.req=null;
this.url=url;
//This callback function is in the data Update function on the page;
this.onload=function(){
//domEl is the dom element with ID #test;
var domEl=document.getElementById("test");
//In addition to using 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");

In main.php, I set a relatively simple sample code here: a message similar to: now! time is:05:18:10 am 2011 will be displayed on the page, which can be changed dynamically time.
Copy code The code is as follows:

echo "now! time is:".date(" H:i:s a Y");
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!