Ajax跨域代理访问网络资源的实现代码(2)
if(this.data==""){this.data = datasetstr;}else{if(datasetstr!="")this.data+= "&" + datasetstr;}
if(this.data=="")this.data=null;
//this.url += ((this.url.indexOf("?")<0) ? "?" : "&") + "jornd=" + this.getrnd();
if(this.method=="GET" && this.data!=null) this.url += "&" + this.data;
if(this.method=="POST") this.headers.push("Content-Type:application/x-www-form-urlencoded");
if(!this.charset || this.charset=="") this.charset = "gb2312";
};
_SmartHttp.prototype.header=function(headstr){
if(headstr.indexOf(":")>=0) this.headers.push(headstr);
return this;
};
_SmartHttp.prototype.timeout=function(){
if(arguments.length>4){return this;}
for(var i =0;i<arguments.length;i++){
if(!isNaN(arguments[i])){
this.timeout[i] = parseInt(arguments[i]);
}
}
return this;
};
_SmartHttp.prototype.send=function(){
this.init();
var _http = this.getobj();
if(_http==null){return this;}
try{
_http.setTimeouts(this.timeout[0], this.timeout[1], this.timeout[2], this.timeout[3]);
}catch(ex){}
_http.open(this.method,this.url,false);
if(this.headers.length>0){
for(var i=0;i<this.headers.length;i++){
var Sindex = this.headers[i].indexOf(":");
var key = this.headers[i].substr(0,Sindex);
var value = this.headers[i].substr(Sindex+1);
_http.setRequestHeader(key,value);
}
}
_http.send(this.data);
this.readyState = _http.readyState;
if(_http.readyState==4){
this.status = parseInt(_http.status);
this.http = _http;
this.content = _http.responseBody;
}
return this;
}
_SmartHttp.prototype.getbinary=function(){
return this.content;
};
_SmartHttp.prototype.gettext=function(charset){
try{
return this.b2s(this.content,charset ? charset : this.charset);
}catch(ex){
this.msg = ex.description;
return "";
}
};
_SmartHttp.prototype.getjson=function(charset){
try{
var _json=null;
eval("_json=(" + this.gettext(charset) + ");");
return _json;
}catch(ex){
this.msg = ex.description;
return null;
}
};
_SmartHttp.prototype.getheader=function(key){
if(key){
if(key.toUpperCase()=="SET-COOKIE"){
key = key.replace("-","\-");
var headers = this.http.getAllResponseHeaders();
var regexp = new RegExp("\n" + key + "\:(.+?)\r","ig");
var resstr = "";
while((res = regexp.exec(headers))!=null){
var val = res[1].trim();
resstr = resstr + val.substr(0,val.indexOf(";")) + "; "
}
if(resstr!=""){
resstr = resstr.substr(0,resstr.lastIndexOf(";"));
}
return resstr;
}else{
return this.http.getResponseHeader(key);
}
}else{return this.http.getAllResponseHeaders();}
};
_SmartHttp.prototype.getxml=function(charset){
try{
var _dom = new ActiveXObject("MSXML2.DOMDocument");
_dom.loadXML(this.gettext(charset));
return _dom;
}catch(ex){
this.msg = ex.description;
return null;
}
};
内容版权声明:除非注明,否则皆为本站原创文章。