如何用ajax、asp編寫的查詢程序_AJAX教程
推薦:詳解Ajax標簽導航大結局(XMLHttpRequest對象) 好了,到了ajax關鍵時刻了。 /* =========================================================== * 函數名稱:ajaxUpdater(tarObj,sMethod,URL,parameters) * 參數說明:tarObj - 異步獲取信息希望顯示的目標節點ID * sMethod -
ajaxsearch.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> 新網頁 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="ajaxsearch.js"></script>
</head>
<body>
<div>
<input type="text" id="searchword" onkeydown="if(event.keyCode==13) AjaxSearch();" />
<input type="button" onclick="AjaxSearch();" value="搜索" />
</div>
<div id="search_result" style="background:#666666;width=100">
</div>
</body>
</html>
ajaxsearch.asp
<!-- #include file="commond.asp" -->
<%
' commond.asp為數據庫連接文件
' function.asp中有要用到的函數CheckStr
Dim Search_Word,XML_Result,rsSearch,sqlSearch
Set rsSearch=Server.CreateObject("ADODB.RecordSet")
' 獲取搜索關鍵字
Search_Word=Request("searchword")
' XML文檔頭
XML_Result="<?xml version=""1.0"" encoding=""utf-8""?><blogsearch>"
IF Search_Word<>Empty Then
' 創建查詢SQL語句
sqlSearch="SELECT log_ID,log_Title,log_Content FROM blog_Content WHERE log_Title LIKE '%"&Search_Word&"%'"
' 打開記錄集
rsSearch.open sqlSearch,Conn,1,1
' 如果沒有搜索結果就產生一個結果,logid為#,標志著沒有搜索結果
IF rsSearch.BOF AND rsSearch.EOF Then XML_Result=XML_Result&"<result><log_id>#</log_id><log_title /></result>"
' 循環輸出搜索結果
Do While Not rsSearch.EOF
XML_Result=XML_Result&"<result><log_id>"&rsSearch("log_ID")&"</log_id><log_title>"&rsSearch("log_Title")&"</log_title></result>" ' 循環輸出每一個結果
rsSearch.MoveNext
Loop
Else
' 關鍵字為空,則返回無搜索結果
XML_Result=XML_Result&"<result><logid>#</logid><logtitle /></result>"
End IF
XML_Result=XML_Result&"</blogsearch>"
' 設置MIME Type為XML文檔
Response.ContentType = "application/xml"
'Response.CharSet = "utf-8"
' 輸出搜索結果
Response.Write(XML_Result)
%>
ajaxsearch.js
var xmlObj = false;
var xmlResult;
try {
xmlObj=new XMLHttpRequest;
}
catch(e) {
try {
xmlObj=new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e2) {
try {
xmlObj=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e3) {
xmlObj=false;
}
}
}
if (!xmlObj) {
alert("XMLHttpRequest init Failed!");
}
function AjaxSearch() {
var searchword;
// 獲取搜索關鍵字,并且進行URLEncode
searchword=escape(document.getElementById("searchword").value);
if(searchword=="") {
// 如果關鍵字為空,則提示用戶輸入關鍵字
document.getElementById("search_result").innerHTML="<ul><li>請輸入關鍵字!</li></ul>";
return;
}
// 給出提示,正在搜索
document.getElementById("search_result").innerHTML="<ul><li>正在加載,請稍候</li></ul>";
// 打開一個連接,采用POST
xmlObj.open ("POST", "ajaxsearch.asp", true);
// 設置請求頭,表單內容格式為URLEncoded
xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// 設置完成請求后響應函數
xmlObj.onreadystatechange=function() {
// 完成響應
if(xmlObj.readyState==4) {
// 狀態正常
if(xmlObj.status==200) {
// 設置xmlResult為搜索結果XML文檔
xmlResult=xmlObj.responseXML;
// 調用AjaxShowResult()顯示搜索結果
AjaxShowResult();
}
}
}
// 發送請求,內容為搜索的關鍵字
xmlObj.send("searchword="+searchword);
}
function AjaxShowResult() {
var results,i,strTemp;
// 獲取搜索結果集合
results=xmlResult.getElementsByTagName("result");
// 用無序列表來顯示搜索結果
strTemp="<ul>";
// 首先判斷搜索結果是否為空
if(results[0].getElementsByTagName("log_id")[0].firstChild.data=="#")
// 是空,則顯示沒有符合的搜索結果
strTemp=strTemp+"<li>無搜索結果</li>";
else
// 循環輸出每個搜索結果
for(i=0;i<results.length;i++)
strTemp = strTemp + "<li><a href='blogview.asp?log_ID=" + results[i].getElementsByTagName("log_id")[0].firstChild.data + "'>" + results[i].getElementsByTagName("log_title")[0].firstChild.data + "</a></li>";
strTemp=strTemp+"</ul>";
// 顯示搜索結果
document.getElementById("search_result").innerHTML = strTemp
}
分享:解讀AJAX的跨域名訪問標題有些唬人的成分,因為這里跨的只是子域名。 事情的經過是這樣的,還是那個個人門戶網站。其中有個功能就是RSS訂閱,每個訂閱作為一個模塊出現在頁面上。如果一個用戶訂閱了比較多的RSS,則在打開頁面時所有的RSS模塊就會開始加載,這時候可能就會需要十
- 相關鏈接:
- 教程說明:
AJAX教程-如何用ajax、asp編寫的查詢程序。