asp如何用ajax无刷新获取数据库数据?
发布网友
发布时间:2022-04-20 20:53
我来回答
共3个回答
热心网友
时间:2022-04-20 22:23
以下只是我随手写的一个基本能实现功能的标准框架而已,返回数据采用json格式。至于程序的优化和兼容性问题请慢慢完善它,能成为一个稳定和扩展性强的框架的。
---------HTML页面-----------------
<script language="javascript">
var JsonArray= new Array()
function AjaxGetData( num ){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET","getData.asp?num="+num,true);
xmlHttp.onreadystatechange =function(){
var JsonData = xmlHttp.responseText
eval( "JsonArray=" + JsonData );
document.getElementById('Number').innerHTML=JsonArray["number"];
document.getElementById('Name').innerHTML=JsonArray["name"];
}
xmlHttp.send(null);
}
</script>
<input onblur="AjaxGetData(this.value)"/>
<table>
<tr>
<th> Number </th>
<th> Name </th>
</tr>
<tr>
<td><span id="Number"></span></td>
<td>><span id="Name"></span></td>
</tr>
</table>
-------------后台读取数据库ASP页面-----------------------
<%
dim cn , rs , num ,name ,jsonString
cn = Server.CreateObject("adodb.connection")
cn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("MyAccessDB.mdb")
rs = cn.Execute "select * from Table where number=" + Request("num")
if not rs.EOF then
num = rs("number")
name = rs("name")
end if
jsonString = "{'number':'?num' , 'name':'?name'}"
jsonString = Replace( jsonString , "?num" , num)
jsonString = Replace( jsonString , "?name" , name)
Response.Write jsonString
%>
热心网友
时间:2022-04-20 23:41
//执行页面test.asp
<%
response.charset = "GB2312"
%>
<script type="text/javascript">
if(typeof XMLHttpRequest == "undefined" && window.ActiveXObject) {
function XMLHttpRequest() {
var a = ["MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0; i<a.length; i++) {
try{
var oXMLHTTP = new ActiveXObject(a[i]);
return oXMLHTTP;
}catch(oError) {}
}
throw new Error("XMLHTTP");
}
}
function upperCase(ref) {
var oHTTP = XMLHttpRequest();
var path = "test2.asp"; //处理数据的网页
var path += "?num=" + ref.value;
oHTTP.open("get", path, true);
oHTTP.onreadystatechange = function() {
if(oHTTP.readyState == 4 && oHTTP.status == 200) {
document.getElementById("nameID").innerHTML = oHTTP.responseText;
}
};
oHTTP.send(null);
}
</script>
<td><input type=text name="number" id="number" onblur="upperCase(this)"></td>
<td align=left width="31%" id="nameID">"name"的值</td>
//处理页面test2.asp
<!--#include file="coon.asp" -->
<%
response.charset = "GB2312"
dim sName
sName = conn.execute("select name from 表名 where number = " & request("num"))(0)
response.write(sName)
%>
热心网友
时间:2022-04-21 01:15
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" EnableScriptGlobalization="true"
EnableScriptLocalization="true" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
//在这对标签里放入你的控件 另外要焦点离开时显示数据库的内容在TextChange事件里写入读取数据库的代码就可以了
</asp:UpdatePanel>