AJAX 簡單類及例子

 

ajax.js:

function Ajax(fnBefore,fnAfter,fnTimeout,iTime,bSync){
 this.before  = fnBefore;
 this.after  = fnAfter;
 this.timeout = fnTimeout;
 this.time  = iTime ? iTime : 900000;
 this.async  = bSync ? false : true;
 this._request = null;
 this._response = null;
}

Ajax.prototype = {
 formatParam : function( data ){
  if ( ! data || typeof data != "object" ) return data;
  var k,r = [];
  for ( k in data ) {
   r.push([k,'=',encodeURIComponent(data[k])].join(''));
  }
  return r.join('&');
 },

 create : function(){
  if(window.XMLHttpRequest) {
   this._request = new XMLHttpRequest();
   if(this._request.overrideMimeType) {
    this._request.overrideMimeType('text/xml');
   }
  } else if(window.ActiveXObject) {
   var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
   for(var i=0; i<versions.length; i++) {
    try {
     this._request = new ActiveXObject(versions[i]);     
    } catch(e) {}
   }
  }
  
 },

 send : function(url,data,method,backtype,ifCache){
  if ( typeof this.before == "function" ) this.before();

  method = method.toUpperCase();
  this.create();

  var self = this;
  var timer = setTimeout(function(){
    if ( typeof self.timeout == "function" ) self.timeout();
    if ( self._request ) {
     self._request.abort();
     self._request = null;
    }
    return true;
   },this.time);

  var sendBody  = this.formatParam(data);

  if ( 'GET' == method ) {
   url = [url, ( url.indexOf('?') == -1 ? '?' : '&') ,sendBody].join('');
   sendBody = null;
  }

  if ( ! ifCache ) {
   url = [url, ( url.indexOf('?') == -1 ? '?' : '&') , "ajaxtimestamp=" , (new Date()).getTime()].join('');
  }

  this._request.open(method,url,this.async);
  if ( "POST" == method ) this._request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  this._request.onreadystatechange = function(){
   if( self._request.readyState==4 ){
    if ( self._request.status==200 ){
     if ( timer ) clearTimeout(timer);
     if(backtype==2){
     self._response = self._request.responseXML;
     }else{
     self._response = self._request.responseText; 
     }
     if ( typeof self.after == "function") self.after(self._response);
    }
   }
  }
  this._request.setRequestHeader("If-Modified-Since","0");
  this._request.send( sendBody );
 },

 get : function(url,data,e,loading,ifCache){
   if ( typeof e == "string" ) e = document.getElementById(e);
   if ( loading ) {
    var rg = /\.(gif|jpg|jpeg|png|bmp)$/i;
    if ( rg.test(loading) ){
     loading = ['<img src="', loading , '"  align="absmiddle" />'].join('');
    }
    this.before = function(){e.innerHTML = loading;}
   }
   this.after  = function(s){e.innerHTML = s;}
   this.timeout = function(){e.innerHTML = ' 請求超時! ';}
   this.send(url,data,"GET",ifCache ? true : false);
 }
};

 

/*
function $(s){
 return document.getElementById(s);
}

function jsonDecode(s){
 return (new Function("return " + s))();
}

function ajax_do_select2(){
    var j_sn = $('data').value;
 (new Ajax(function(){
 $("log").innerHTML = "<img src='images/aloading.gif' />正在獲取數據,請稍後...";
 },function(s){
 alert(s);
 $("log").innerHTML=s;
 },function(){
  $("log").innerHTML = "數據請求超時...";
 },0) ).send("ajax_log.php",{v_sn:j_sn},"POST",1);
}
*/

 

返回XML頁面:

<?php
header('Content-Type:text/xml;charset=big5');
include '../../dblink.php';

echo '<?xml version="1.0" encoding="BIG5" standalone="yes" ?>';
echo '<xml_value>';
echo   '<x_num1>'.$v_num1.'</x_num1>
  <x_num2>'.$v_num2.'</x_num2>
  <x_num3>'.$v_num3.'</x_num3>
  <x_num4>'.$v_num4.'</x_num4>
  <x_num5>'.$v_num5.'</x_num5>
  <x_num6>'.$v_num6.'</x_num6>';
echo '</xml_value>';
 ?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章