日期:2014-05-16 浏览次数:20466 次
function Ajax(method,url,flag,content,type,charset){
this.method = method;
this.url = url;
this.flag = flag;
this.content = content;
this.type = type;
this.charset = charset;
this.header = null;
this.value = null;
var xmlHttp = null;
{
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType(type);
}
}catch (e){
// Internet Explorer
try{
for( var i = 5; i; i-- ){
try{
if( i != 2 ){
xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
}
xmlHttp.setRequestHeader("Content-Type",type);
xmlHttp.setRequestHeader("Content-Type",charset);
break;
}catch(e){
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
}catch(e2){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e3){
alert("您的浏览器不支持AJAX!");
xmlHttp = null;
}
}
}
this.xmlHttp = xmlHttp;
}
this.onReady = function(xmlHttp){
xmlHttp.onreadystatechange=function(){
//alert(xmlHttp.readyState);
if(xmlHttp.readyState==0){
//请求未初始化(在调用 open() 之前)
//alert("请求未初始化(在调用 open() 之前)");
}else if(xmlHttp.readyState==1){
//请求已提出(调用 send() 之前)
//alert("请求已提出(调用 send() 之前)");
}else if(xmlHttp.readyState==2){
// 请求已发送(这里通常可以从响应得到内容头部)
//alert("请求已发送(这里通常可以从响应得到内容头部)");
}else if(xmlHttp.readyState==3){
// 请求处理中(响应中通常有部分数据可用,但是服务器还没有完成响应)
//alert("请求处理中(响应中通常有部分数据可用,但是服务器还没有完成响应)");
}else if(xmlHttp.readyState==4){
// 请求已完成(可以访问服务器响应并使用它)
//alert("请求已完成(可以访问服务器响应并使用它)");
if (xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}
}
}
//终止当前请求;
this.sTop =function(){
xmlHttp.abort();
}
//把HTTP所以响应首部作为键/值对返回;
this.getAllResponseHeaders=function(){
return xmlHttp.getAllResponseHeaders();
}
//返回指定首部的串值;
this.getResponseHeader=function (header){
return xmlHttp.getResponseHeader(header);
}
//把指定首部设置为所提供的值。在设置任何首部之前必须先调用open()
this.setRequestHeader = function(header,value){
xmlHttp.setRequestHeader(header,value);
}
//处理下默认参数不存在的情况
this.process = function(){
if(this.flag==null||this.flag=="undefined"||this.flag==""){
this.flag = true;
}
if(this.content==null||this.content=="undefined"||this.content==""){
this.content = null;
}
if(this.type==null||this.type=="undefined"||this.type==""){
this.type = "text/xml";
}
if(this.charset==null||this.charset=="undifind"||this.charset==""){
this.charset = "utf-8";
}
if(this.method==null||this.method=="undefined"||this.method==""){
this.method = "POST";
}
}
//一个是指示所用方法(通常是GET或POST,PUT很少用)的串,另一个是表示目标资源URL的串,
//还有一个Boolean值,指示请求是否是异步的。
this.processRequest = function(){
this.process();
if(xmlHttp!=null){
this.onReady(xmlHttp);
xmlHttp.open(this.method,this.url,this.flag);
xmlHttp.send(this.content);
}else{
alert("您的浏览器不支持ajax!");
}
}
//------------无参数-----------------------
this.request=function(){
this.processRequest();
}
//------------有参数-----------------------
this.requestWithParams=function(method,url,flag,content,type,charset){
this.method = method;
this.url = url;
this.flag = flag;
this.content = content;
this.type = type;
this.charset = charset;
this.processRequest();
}
}
<script type="text/javascript" language=