摘抄:Using the XML HTTP Request object 

2007/1/15 21:23:41


阅读全文(1991) | 回复(0) | 编辑 | 精华

1、Creating the object In Internet Explorer, you create the object using new ActiveXObject("Msxml2.XMLHTTP") or new ActiveXObject("Microsoft.XMLHTTP") depending on the version of MSXML installed. In Mozilla and Safari (and likely in future UA's that support it) you use new XMLHttpRequest() IceBrowser uses yet another method the window.createRequest() method. var xmlhttp=false;/*@cc_on @*//*@if (@_jscript_version >= 5)// JScript gives us Conditional compilation, we can cope with old IE versions.// and security blocked creation of the objects. try {  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {  try {   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  } catch (E) {   xmlhttp = false;  } }@end @*/if (!xmlhttp && typeof XMLHttpRequest!='undefined') { try {  xmlhttp = new XMLHttpRequest(); } catch (e) {  xmlhttp=false; }}if (!xmlhttp && window.createRequest) { try {  xmlhttp = window.createRequest(); } catch (e) {  xmlhttp=false; }} 2、How do I make a request? xmlhttp.open("GET", "test.txt",true); xmlhttp.onreadystatechange=function() {  if (xmlhttp.readyState==4) {   alert(xmlhttp.responseText)  } } xmlhttp.send(null) 3、Making a HEAD request  xmlhttp.open("HEAD", "/faq/index.html",true); xmlhttp.onreadystatechange=function() {  if (xmlhttp.readyState==4) {   alert(xmlhttp.getAllResponseHeaders())  } } xmlhttp.send(null) 4、Using HEAD requests, to find the Last-Modified of another file.  xmlhttp.open("HEAD", "/faq/index.html",true); xmlhttp.onreadystatechange=function() {  if (xmlhttp.readyState==4) {   alert("File was last modified on - "+    xmlhttp.getResponseHeader("Last-Modified"))  } } xmlhttp.send(null) 5、Does a url exist? xmlhttp.open("HEAD", "/faq/index.html",true); xmlhttp.onreadystatechange=function() {  if (xmlhttp.readyState==4) {   if (xmlhttp.status==200) alert("URL Exists!")    else if (xmlhttp.status==404) alert("URL doesn't exist!")     else alert("Status is "+xmlhttp.status)  } } xmlhttp.send(null) 6、Calling a server-side Script without refreshing the page <% a=+(Request.QueryString('a')+'') b=+(Request.QueryString('b')+'') if (isNaN(a) || isNaN(b)) {a='';b='';total='' }  else {   total=a+b  } acc=Request.ServerVariables('HTTP_ACCEPT')+'' if (acc.indexOf('message/x-jl-formresult')!=-1) {  Response.Write(total) } else {%><script src="xmlhttp.js" type="text/javascript"></script><script> function calc() {  frm=document.forms[0]  url="add.1?a="+frm.elements['a'].value+"&b="+frm.elements['b'].value  xmlhttp.open("GET",url,true);  xmlhttp.onreadystatechange=function() {   if (xmlhttp.readyState==4) {    document.forms[0].elements['total'].value=xmlhttp.responseText   }  } xmlhttp.setRequestHeader('Accept','message/x-jl-formresult') xmlhttp.send() return false}</script><form action="add.1" method="get" onsubmit="return calc()"><input type=text name=a value="<%=a%>"> + <input type=text name=b value="<%=b%>"> = <input type=text name=total value="<%=total%>"><input type=submit value="Calculate"></form><%}%> 7、Using JSON as the transfer language xmlhttp.open("GET","/routeplanner/airport.1?LHR",true); xmlhttp.onreadystatechange=function() {  if (xmlhttp.readyState==4) {   if (xmlhttp.status!=404) {    var local=new Function("return "+xmlhttp.responseText)();    alert("Code - Name\n"+local[0].id+' - '+local[0].name);   } else {    alert("Airport not found");   }  } } xmlhttp.send(null); 8、Using XMLHTTP with GOOGLE's SOAP API  search="Word" xmlhttp.open("POST", "http://api.google.com/search/beta2",true); xmlhttp.onreadystatechange=function() {  if (xmlhttp.readyState==4) {   alert(xmlhttp.responseText)  } } xmlhttp.setRequestHeader("Man", "POST http://api.google.com/search/beta2 HTTP/1.1") xmlhttp.setRequestHeader("MessageType", "CALL") xmlhttp.setRequestHeader("Content-Type", "text/xml")  xmlhttp.send("<?xml version='1.0' encoding='UTF-8'?>"+"\n\n"+"<SOAP-ENV:Envelope"+      ' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"'+      ' xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"'+      ' xmlns:xsd="'">http://www.w3.org/1999/XMLSchema">'+      '<SOAP-ENV:Body><ns1:doGoogleSearch'+      ' xmlns:ns1="urn:GoogleSearch"'+      ' SOAP-ENV:encodingStyle="'">http://schemas.xmlsoap.org/soap/encoding/">'+      '<key xsi:type="xsd:string">GOOGLEKEY</key> <q'+      ' xsi:type="xsd:string">'+search+'</q> <start'+      ' xsi:type="xsd:int">0</start> <maxResults'+      ' xsi:type="xsd:int">10</maxResults> <filter'+      ' xsi:type="xsd:boolean">true</filter> <restrict'+      ' xsi:type="xsd:string"></restrict> <safeSearch'+      ' xsi:type="xsd:boolean">false</safeSearch> <lr'+      ' xsi:type="xsd:string"></lr> <ie'+      ' xsi:type="xsd:string">latin1</ie> <oe'+      ' xsi:type="xsd:string">latin1</oe>'+      '</ns1:doGoogleSearch>'+    '</SOAP-ENV:Body></SOAP-ENV:Envelope>') ================================================http://www.jibbering.com/2002/4/httprequest.html

Qr

Posted by Qr on 2007/1/15 21:23:41


发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.094 second(s), page refreshed 144771849 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号