Home  >  Article  >  Web Front-end  >  Share examples of implementing ajax encapsulation by yourself_javascript skills

Share examples of implementing ajax encapsulation by yourself_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:53:531004browse

Copy code The code is as follows:

//javascript Object: ajax Object
// Created By RexLee
function Ajax(url,data){
this.url=url;
this.data=data;
this.browser=(function(){
if(navigator .userAgent.indexOf("MSIE")>0) {
                                                                                                                    ();
};
Ajax.prototype={
get:function(){
var result;
var xmlhttp;
if(this.browser=='MSIE' ){
try{
xmlhttp=new ActiveXObject('microsoft.xmlhttp');
}catch(e){
xmlhttp=new ActiveXObject ('msxml2.xmlhttp');
                                      
                                                                                                           = xmlhttp.responseText;//Closure, this cannot be used. Attributes
};
xmlhttp.open('GET',this.url '?' this.data,false);//true cannot fetch data, why?

return result;
},
post:function(){
var result;
var xmlhttp;
if(this.browser=='MSIE'){
xmlhttp=new ActiveXObject ('microsoft.xmlhttp'); {
result = xmlhttp .responseText;//Closure, cannot use this. attribute
       }; > xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//In POST, this sentence must be
xmlhttp.send(this.data);
}
};

//var a=new ajax('opp2.js','');
//alert('by GET\n' a.get( ))
//alert('by POST\n' a.post());
////////////////////////// //////



window.onload=function(){
document.getElementById("btn").onclick=function(){
var p= document.getElementById("t").value;
var a=new Ajax("phpOOP/getPage.php","page=" p);
document.getElementById("box").innerHTML=a .get();
};
}


Statement:
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