在不刷新页面的情况下调用远程asp脚本
2018-09-06 11:01
1.请在sqlserver中建一数据库name:house在house中运行下列脚本
ifexists(select*fromsysobjectswhereid=object_id(N´[dbo].[City]´)andOBJECTPROPERTY(id,N´IsUserTable´)=1)
droptable[dbo].[City]
GO
ifexists(select*fromsysobjectswhereid=object_id(N´[dbo].[Area]´)andOBJECTPROPERTY(id,N´IsUserTable´)=1)
droptable[dbo].[Area]
GO
CREATETABLE[dbo].[City](
[id][int]IDENTITY(1,1)NOTNULL,
[name][nvarchar](10)NULL
)ON[PRIMARY]
GO
CREATETABLE[dbo].[Area](
[id][int]IDENTITY(1,1)NOTNULL,
[CityId][int]NULL,
[name][nvarchar](20)NULL
)ON[PRIMARY]
GO
INSERTINTOCity(id,name)Values(1,广州)
INSERTINTOCity(id,name)Values(2,上海)
INSERTINTOCity(id,name)Values(3,北京)
INSERTINTOArea(id,CityId,name)Values(1,1,天河)
INSERTINTOArea(id,CityId,name)Values(2,1,白云)
INSERTINTOArea(id,CityId,name)Values(3,2,虹口)
INSERTINTOArea(id,CityId,name)Values(4,2,宝山)
INSERTINTOArea(id,CityId,name)Values(5,2,静安)
INSERTINTOArea(id,CityId,name)Values(6,3,海淀)
INSERTINTOArea(id,CityId,name)Values(7,3,朝阳)
2.建立文件
建立文件CityData.asp
<%@LANGUAGE=VBSCRIPT%>
<%RSDispatch%>
<SCRIPTRUNAT=SERVERLanguage=javascript>
<!--#INCLUDEVIRTUAL=/_ScriptLibrary/RS.ASP-->
<!--#INCLUDEVIRTUAL=/jsconn.asp-->
functionDescription()
{
this.GetAreaAsArray=DoGetData;
}
public_description=newDescription();
functionDoGetData(cityId)
{
varsql,rst,strText,conn,i,j
conn.ConnectionString=connstr;
conn.ConnectionTimeout=30;
conn.Open();
sql=Selectname,idfromareawherecityid=´+cityId+´;
rst=newActiveXObject(ADODB.Recordset);
rst.CursorLocation=3;
rst.Open(sql,conn);
i=0;
j=0;
strText=newArray();
if(rst.RecordCount>=1)
{
for(vari=0;i<rst.RecordCount;++i)
{
strText[j++]=rst.Fields(name).Value;
strText[j++]=rst.Fields(id).Value;
rst.movenext();
}
}
else
{
strText[j++]=没有;
strText[j++]=1;
}
rst.close();
conn.close();
returnstrText;
}
</SCRIPT>
建立文件jsconn.asp
<SCRIPTRUNAT=SERVERLanguage=javascript>
varconnstr=driver={SQLServer};server=202.0.0.108;uid=sa;pwd=;database=house;
</SCRIPT>
建立文件pub.asp
<%
FunctionOpenOrGet_Database
DimSessionName,conn
constconnstr=driver={SQLServer};server=202.0.0.108;uid=sa;pwd=;database=house
SessionName=House
IfNotIsObject(Session(SessionName))Then
Setconn=Server.CreateObject(ADODB.Connection)
conn.Openconnstr
SetSession(SessionName)=conn
EndIf
SetOpenOrGet_Database=Session(SessionName)
EndFunction
%>
建立文件sql_pub.asp
<%
´取出城市资料
FunctionSelectCity()
DimConn,Sql,Rs,ArrCity,TmpArr(1,0)
SetConn=OpenOrGet_Database
SetRs=Server.CreateObject(ADODB.Recordset)
Sql=Selectdistinctname,idfromCity
Rs.OpenSql,Conn,3
ifRs.Eofthen
TmpArr(0,0)=城市
TmpArr(1,0)=0
Rs.Close
SelectCity=TmpArr
else
ArrCity=RS.GetRows()
Rs.Close
SelectCity=ArrCity
endif
EndFunction
´根据传来的CityId取出相应的地区资料
FunctionSelectArea(CityId)
DimConn,Sql,Rs,ArrArea,TmpArr(1,0)
SetConn=OpenOrGet_Database
SetRs=Server.CreateObject(ADODB.Recordset)
Sql=Selectname,idfromAreawhereCityId=&CityId&orderbyid
Rs.OpenSql,Conn,3
ifRs.Eofthen
TmpArr(0,0)=地区
TmpArr(1,0)=0
Rs.Close
SelectArea=TmpArr
else
ArrArea=RS.GetRows()
Rs.Close
SelectArea=ArrArea
endif
EndFunction
%>
建立文件test.asp
<%@LANGUAGE=VBSCRIPT%>
<%optionexplicit%>
<!--#includeVIRTUAL=pub.asp-->
<!--#includeVIRTUAL=Sql_pub.asp-->
<%
DimarrCity,arrArea,strPathInfo,strServerName,strServerPort,i,TmpStr
strPathInfo=Request.ServerVariables(PATH_INFO)
strServerName=Request.ServerVariables(SERVER_NAME)
strServerPort=Request.ServerVariables(SERVER_PORT)
iflen(strServerPort)=0then
strServerPort=
else
strServerPort=:+strServerPort
endif
strPathInfo=
arrCity=SelectCity()
arrArea=SelectArea(arrCity(1,0))
%>
<html>
<head>
<title>UntitledDocument</title>
<metahttp-equiv=目录类型content=文本/html;字符集=gb2312>
</head>
<scriptlanguage=JavaScriptsrc=<%=strPathInfo%>/_ScriptLibrary/rs.htm></script>
<scriptlanguage=JavaScript>
RSEnableRemoteScripting(<%=strPathInfo%>/_ScriptLibrary);
</script>
<bodybgcolor=#FFFFFF>
<formmethod=postname=select>
<fontcolor=#222200>
<selectname=Citysize=1onChange=JavaScript:Choosesection(this.form.Area,this.form.City)>
<optionvalue=0selected>--选择城市--</option>
<%fori=LBound(arrCity,2)ToUBound(arrCity,2)
TmpStr=<optionvalue=&arrCity(1,i)&
TmpStr=TmpStr&>&arrCity(0,i)&</option>
Response.Write(TmpStr)
Next
%>
</select>
</font><fontcolor=#222200>
<selectname=Areasize=1>
<optionvalue=0>--选择镇区--</option>
<%Fori=LBound(arrArea,2)ToUBound(arrArea,2)
ifarrArea(1,i)<>0then
%>
<optionvalue=<%=arrArea(1,i)%>><%=arrArea(0,i)%></option>
<%endif
Next%>
</select>
</font>
</form>
<scriptlanguage=JavaScript>
varserverURL=<%=strPathInfo%>;
varpageURL=/CityData.asp;
functionChoosesection(oArea,oCity)
{
for(vari=oArea.options.length-1;i>0;--i)
{oArea.options.remove(i)}
varCityId=oCity.options(oCity.selectedIndex).value;
varobjResult=RSExecute(serverURL+pageURL,GetAreaAsArray,CityId);
varj=0
if(objResult.return_value.length>0)
{
for(vari=0;i<objResult.return_value.length/2;++i)
{
sName=objResult.return_value[j++];
sId=objResult.return_value[j++];
varoOption=document.createElement(´OPTION´);
oOption.text=sName;
oOption.value=sId;
oArea.options.add(oOption);
}
}
}
</script>
</body>
</html>
文章标题:在不刷新页面的情况下调用远程asp脚本
文章链接:http://soscw.com/index.php/essay/8951.html