建站代码网

热门标签

AJAX and 经典的ASP.

使用javascript中的xmlHTTP服务器对象请求包含脚本的ASP页来输出文本。


更新!本文使用jQuery提供了新的版本。


通用跨浏览器JS函数创建XMLHTTPSServer对象。为了清楚地介绍Ajax的基本知识,这一系列文章特别有用:

http:///developerworks/web/library/wa-ajaxintro1.html.


function GetXmlHttpObject(handler) { var objXmlHttp=null; if (navigator.userAgent.indexOf("MSIE")>=0) { var strName="Msxml2.XMLHTTP"; if (navigator.appVersion.indexOf("MSIE 5.5")>=0) { strName="Microsoft.XMLHTTP"; } try { objXmlHttp=new ActiveXObject(strName); objXmlHttp.onreadystatechange=handler; return objXmlHttp; } catch(e) { alert("Error. scripting for ActiveX might be disabled"); return; } } if (navigator.userAgent.indexOf("Mozilla")>=0) { objXmlHttp=new XMLHttpRequest(); objXmlHttp.onload=handler; objXmlHttp.onerror=handler; return objXmlHttp; } }


在HTML选择元素上由选定索引更改触发的示例JS函数。这一个将Cuffer-ID附加到将被执行的ASP脚本的QueqSnpe。第一个JS函数将调用第二个StistC改函数,它将输出任何返回的字符串(响应脚本)到div的内层HTML。


function GetCustomer(id) { var url="FetchCustomer.asp?CustomerID=" + id ; xmlHttp=GetXmlHttpObject(stateChanged); xmlHttp.open("GET", url , true); xmlHttp.send(null); } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById('CustomerDetails').innerHTML=xmlHttp.responseText; } }


带有Onchange事件的选择列表连接到GETCube函数。


<% strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;" strDb = "Data Source=" & Server.MapPath("Northwind.mdb") strConn = strProvider & strDb Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open strConn Set rs = Conn.Execute("SELECt [CustomerID], [CompanyName] FROM [Customers]") If Not rs.EOF Then %> <form> <select name="CustomerID" onChange="GetCustomer(this.value);"> <option></option> <% Do Until rs.EOF Response.Write "<option value=""" & rs("CustomerID") & """>" Response.Write rs("CompanyName") & "</option>" & VbCrLf rs.MoveNext Loop rs.Close : Set rs = Nothing : Conn.Close : Set Conn = Nothing %> </select> </form> <% Else rs.Close : Set rs = Nothing : Conn.Close : Set Conn = Nothing Response.Write "<p>Something bad went wrong</p>" End If %> <div id="CustomerDetails"></div>


一个示例ASP脚本,它从QuiSQL字符串中接收值并查询数据库,将结果输出到HTMLHTTP响应,由XMLHTTP对象接收并更改其RealyStand属性,并启动StaseC改方法,该方法将响应集写入到div中。


<%@LANGUAGE="VBscript" CODEPAGE="1252"%> <% strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;" strDb = "Data Source=" & Server.MapPath("Northwind.mdb") strConn = strProvider & strDb Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open strConn query = "SELECT * FROM Customers WHERe CustomerID = ?" CustomerID = Request.QueryString("CustomerID") arParams = array(CustomerID) set cmd = Server.CreateObject("ADODB.Command") cmd.CommandText = query set cmd.ActiveConnection = Conn Set rs = cmd.Execute(,arParams,1) If Not rs.EOF Then Response.Write rs(1) & "<br />" & _ rs(4) & "<br />" & _ rs(5) & "<br />" & _ rs(6) & "<br />" & _ rs(7) & "<br />" & _ rs(8) & "<br />" & _ "Tel: " & rs(9) & "<br />" End If rs.Close : Set rs = Nothing set cmd = Nothing Conn.Close : Set Conn=nothing %>


上面的示例包括使用传递给命令的执行方法的参数数组。这有助于防止SQL注入。


X