偶尔一笔
谢勇
三 xmldom 的使用方法
IE7, Mozilla ,Firefox等浏览器中 javascript是内置有 XMLHttpRequest 这个对象的 但IE5+则没有 需要用如下方法来启动 takko fashion //IE 6 try{ xhttp = new ActiveXObject("Msxml2.XMLHTTP";} catch(e){ ; } //IE5+ if(xhttp takko fashion == null) try { xhttp = new ActiveXObject("Microsoft.XMLHTTP";} catch(e){ ; }
参数三 true 表示异步传输 服务器返回信息完成前 你可以进行其它操作 false 表示阻断方式的传输
[1] 如果用阻断的方式来发送请求 那么直接用 if(xhttp.readyState == 4)就能判断是否完成
xhttp.onreadystatechange = function() { //这里来进行上面阻断方式的判断 if(myajax.xhttp.readyState == 4){ takko fashion takko fashion if(myajax.xhttp.status == 200){ //要进行的后续操作 } takko fashion } }
//初始化xmlhttp if(window.XMLHttpRequest){//IE7, Mozilla ,Firefox 等浏览器内置该对象 this.xhttp = new XMLHttpRequest(); }else if(window.ActiveXObject){//IE6 IE5 try { this.xhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) { } if (this.xhttp == null) try { this.xhttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { } } this.xhttp.onreadystatechange takko fashion = WiteOKFunc; //rs: responseBody responseStream responseXml responseText
//初始化xmldom this.InitXDom = function(){ var obj = null; if (typeof(DOMParser) != "undefined") takko fashion { // Gecko Mozilla Firefox var parser = new DOMParser(); obj = parser.parseFromString(xmlText, "text/xml"); } else { // IE try { obj = new ActiveXObject("MSXML2.DOMDocument");} catch (e) { } if (obj == null) try { obj = new ActiveXObject("Microsoft.XMLDOM"); } catch (e) { } } this.xdom = obj; };
//增加一个POST或GET键值对 this.AddKey = function(skey,svalue){ this.keyCount++; this.keys[this.keyCount] = skey; this.values[this.keyCount] = escape(svalue); };
//增加一个Http请求头键值对 this.AddHead = function(skey,svalue){ this.rkeyCount++; this.rkeys[this.rkeyCount] = skey; takko fashion this.rvalues[this.rkeyCount] = svalue; };
//清除当前对象的哈希表参数 this.ClearSet = function(){ this.keyCount = -1; takko fashion takko fashion this.keys = Array(); this.values = Array(); this.rkeyCount = -1; this.rkeys = Array(); this.rvalues = Array(); };
//发送http请求头 this.SendHead = function(){ if(this.rkeyCount!=-1){ //发送用户自行设定的请求头 takko fashion for(;i<=this.rkeyCount;i++){ this.xhttp.setRequestHeader(this.rkeys[i],this.rvalues[i]); } } if(this.rtype=='binary'){ this.xhttp.setRequestHeader("Content-Type","multipart/form-data"); }else{ this.xhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); } };
//用Post方式发送数据 this.SendPost = function(purl){ var pdata = ""; takko fashion var i=0; this.state = 0; this.xhttp.open("POST", purl, true); this.SendHead(); if(this.keyCount!=-1){ //post数据 for(;i<=this.keyCount;i++){ takko fashion if(pdata=="") pdata = this.keys[i]+'='+this.values[i]; takko fashion else pdata += "&"+this.keys[i]+'='+this.values[i]; } } this.xhttp.send(pdata); };
//用GET方式发送数据 this.SendGet = function(purl){ takko fashion var gkey = ""; &n
No comments:
Post a Comment