项目中AJAX总结

JSP中某方法的调用
function jfdhPlace(code){
    var url="jfdh_exchange.ajax";
        url+="?code="+code;
    var Check=new Ajaxobj(url,placeCallBack,"get","text",null);
}

function placeCallBack(){
var response=eval("("+this.req.responseText+")");

var curpage=0;
var bakpg=0;
if(response.status){
  alert("兑换成功");
}
else
alert(response.message);
}


JS文件中的关键ajax.js

var READY_STATE_UNINITALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;


function Ajaxobj(url,callback,method,type,content) {
    var now = new Date();
   if(url.indexOf("?") > 0)
     url=url+"&randid="+now.getTime();
   else
     url=url+"?randid="+now.getTime();
  
   this.url=url;
   this.req=null;
   this.callback=callback;
   this.mode=method;
   this.type=type;
   this.content=content;
   this.loadXMLDoc();
}

Ajaxobj.prototype.loadXMLDoc = function(){
if(window.XMLHttpRequest){
this.req=new XMLHttpRequest();
}else if(window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}

if(this.req){
try{
var loader=this;
this.req.onreadystatechange=function(){
loader.onReadyState.call(loader);
}
if(this.method=="post"){
this.req.open("post",this.url,true);
this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.req.send(this.content);
}
else{
this.req.open("get",this.url,true);
this.req.send(null);
}
}catch(e){
    alert("Ajax请求错误 !");
}
}
}

Ajaxobj.prototype.onReadyState = function(){
var req=this.req;
var ready=req.readyState;
if(ready==READY_STATE_COMPLETE){
var httpStatus=req.status;
if(httpStatus==200){
this.callback.call(this);
}
}   
}

你可能感兴趣的:(Ajax,jsp,prototype,Microsoft,Exchange)