如何用ajax、asp編寫的查詢程序_AJAX教程
推薦:詳解Ajax標(biāo)簽導(dǎo)航大結(jié)局(XMLHttpRequest對(duì)象) 好了,到了ajax關(guān)鍵時(shí)刻了。 /* =========================================================== * 函數(shù)名稱:ajaxUpdater(tarObj,sMethod,URL,parameters) * 參數(shù)說明:tarObj - 異步獲取信息希望顯示的目標(biāo)節(jié)點(diǎn)ID * sMethod -
ajaxsearch.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> 新網(wǎng)頁 </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為數(shù)據(jù)庫(kù)連接文件
' function.asp中有要用到的函數(shù)CheckStr
Dim Search_Word,XML_Result,rsSearch,sqlSearch
Set rsSearch=Server.CreateObject("ADODB.RecordSet")
' 獲取搜索關(guān)鍵字
Search_Word=Request("searchword")
' XML文檔頭
XML_Result="<?xml version=""1.0"" encoding=""utf-8""?><blogsearch>"
IF Search_Word<>Empty Then
' 創(chuàng)建查詢SQL語句
sqlSearch="SELECT log_ID,log_Title,log_Content FROM blog_Content WHERE log_Title LIKE '%"&Search_Word&"%'"
' 打開記錄集
rsSearch.open sqlSearch,Conn,1,1
' 如果沒有搜索結(jié)果就產(chǎn)生一個(gè)結(jié)果,logid為#,標(biāo)志著沒有搜索結(jié)果
IF rsSearch.BOF AND rsSearch.EOF Then XML_Result=XML_Result&"<result><log_id>#</log_id><log_title /></result>"
' 循環(huán)輸出搜索結(jié)果
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>" ' 循環(huán)輸出每一個(gè)結(jié)果
rsSearch.MoveNext
Loop
Else
' 關(guān)鍵字為空,則返回?zé)o搜索結(jié)果
XML_Result=XML_Result&"<result><logid>#</logid><logtitle /></result>"
End IF
XML_Result=XML_Result&"</blogsearch>"
' 設(shè)置MIME Type為XML文檔
Response.ContentType = "application/xml"
'Response.CharSet = "utf-8"
' 輸出搜索結(jié)果
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;
// 獲取搜索關(guān)鍵字,并且進(jìn)行URLEncode
searchword=escape(document.getElementById("searchword").value);
if(searchword=="") {
// 如果關(guān)鍵字為空,則提示用戶輸入關(guān)鍵字
document.getElementById("search_result").innerHTML="<ul><li>請(qǐng)輸入關(guān)鍵字!</li></ul>";
return;
}
// 給出提示,正在搜索
document.getElementById("search_result").innerHTML="<ul><li>正在加載,請(qǐng)稍候</li></ul>";
// 打開一個(gè)連接,采用POST
xmlObj.open ("POST", "ajaxsearch.asp", true);
// 設(shè)置請(qǐng)求頭,表單內(nèi)容格式為URLEncoded
xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// 設(shè)置完成請(qǐng)求后響應(yīng)函數(shù)
xmlObj.onreadystatechange=function() {
// 完成響應(yīng)
if(xmlObj.readyState==4) {
// 狀態(tài)正常
if(xmlObj.status==200) {
// 設(shè)置xmlResult為搜索結(jié)果XML文檔
xmlResult=xmlObj.responseXML;
// 調(diào)用AjaxShowResult()顯示搜索結(jié)果
AjaxShowResult();
}
}
}
// 發(fā)送請(qǐng)求,內(nèi)容為搜索的關(guān)鍵字
xmlObj.send("searchword="+searchword);
}
function AjaxShowResult() {
var results,i,strTemp;
// 獲取搜索結(jié)果集合
results=xmlResult.getElementsByTagName("result");
// 用無序列表來顯示搜索結(jié)果
strTemp="<ul>";
// 首先判斷搜索結(jié)果是否為空
if(results[0].getElementsByTagName("log_id")[0].firstChild.data=="#")
// 是空,則顯示沒有符合的搜索結(jié)果
strTemp=strTemp+"<li>無搜索結(jié)果</li>";
else
// 循環(huán)輸出每個(gè)搜索結(jié)果
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>";
// 顯示搜索結(jié)果
document.getElementById("search_result").innerHTML = strTemp
}
分享:解讀AJAX的跨域名訪問標(biāo)題有些唬人的成分,因?yàn)檫@里跨的只是子域名。 事情的經(jīng)過是這樣的,還是那個(gè)個(gè)人門戶網(wǎng)站。其中有個(gè)功能就是RSS訂閱,每個(gè)訂閱作為一個(gè)模塊出現(xiàn)在頁面上。如果一個(gè)用戶訂閱了比較多的RSS,則在打開頁面時(shí)所有的RSS模塊就會(huì)開始加載,這時(shí)候可能就會(huì)需要十
- Ajax中瀏覽器的緩存問題解決方法
- AJAX和WebService實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)具體代碼
- ajax 登錄功能簡(jiǎn)單實(shí)現(xiàn)(未連接數(shù)據(jù)庫(kù))
- AJAX和WebService實(shí)現(xiàn)郵箱驗(yàn)證(無刷新驗(yàn)證郵件地址是否合法)
- AJAX和三層架構(gòu)實(shí)現(xiàn)分頁功能具體思路及代碼
- 使用AJAX返回WebService里的集合具體實(shí)現(xiàn)
- AJAX獲取服務(wù)器當(dāng)前時(shí)間及時(shí)間格式輸出處理
- ajax傳遞多個(gè)參數(shù)具體實(shí)現(xiàn)
- ajax傳遞一個(gè)參數(shù)具體實(shí)現(xiàn)
- 滑輪滾動(dòng)到頁面底部ajax加載數(shù)據(jù)配合jsonp實(shí)現(xiàn)探討
- jQery ajax——load()方法示例介紹
- jQuery+Ajax實(shí)現(xiàn)表格數(shù)據(jù)不同列標(biāo)題排序(為表格注入活力)
- 相關(guān)鏈接:
- 教程說明:
AJAX教程-如何用ajax、asp編寫的查詢程序
。