asp如何用ajax无刷新获取数据库数据?
在asp页面文本输入框输入number,焦点离开时在别的单元格中显示数据库中对应的name的值,number和name分别为字段名,详细的代码,谢谢!谢谢各位老师,但我试...
在asp 页面文本输入框输入number,焦点离开时在别的单元格中显示数据库中对应的name的值,number和name分别为字段名,详细的代码,谢谢!
谢谢各位老师,但我试不出来,不过还是谢谢了,我慢慢琢磨 展开
谢谢各位老师,但我试不出来,不过还是谢谢了,我慢慢琢磨 展开
展开全部
以下只是我随手写的一个基本能实现功能的标准框架而已,返回数据采用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
%>
---------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
%>
展开全部
//执行页面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)
%>
<%
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)
%>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" EnableScriptGlobalization="true"
EnableScriptLocalization="true" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
//在这对标签里放入你的控件 另外要焦点离开时显示数据库的内容在TextChange事件里写入读取数据库的代码就可以了
</asp:UpdatePanel>
EnableScriptLocalization="true" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
//在这对标签里放入你的控件 另外要焦点离开时显示数据库的内容在TextChange事件里写入读取数据库的代码就可以了
</asp:UpdatePanel>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询