在html页面用ajax的简单应用通过javascript得到数据库中的图片路径并在表格中显示出来

    技术2022-05-11  107

     Jscript代码:

    function  getHTTPObject()            {                                 var http;          var browser = navigator.appName;          if(browser == "Microsoft Internet Explorer")           {                //如果用户使用IE,就返回XMLHTTP的ActiveX对象                http = new ActiveXObject("Microsoft.XMLHTTP");           }          else          {                //否则返回一个XMLHttpRequest对象                http = new XMLHttpRequest();           }          return http;    }      var  http  =  getHTTPObject();      // 处理请求状态变化      function  getPicPath()            {           //4表示请求已完成          if (http.readyState == 4)           {//获取服务段的响应文本                var picStr = http.responseText;                 //插入响应到ID为ajax-sample的DIV标签内                document.getElementById("pic").innerHTML = picStr;           }    }      function  picPath(str)     {            var LocString=String(window.document.location.href);        var rs=new RegExp("(^|)"+str+"=([^&]*)(&|$)","gi").exec(LocString),tmp; //取得地址栏中档案ID号        if (tmp=rs){          var url = "add_photo/getpicpath.asp?picid=1&userid="+tmp[2];          //指定服务端的地址          http.open("GET", url, true);           //请求状态变化时的处理函数          http.onreadystatechange = getPicPath;           //发送请求          http.send(null);        }    }

    HTTP代码:<body οnlοad="picPath()">...<div id="pic"></div>...</body>

    ASP代码:

    <% @ Language = VBScript  %> <!--  #INCLUDE FILE = " connectdb.asp "   --> <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > < html xmlns = " http://www.w3.org/1999/xhtml " > < head > < meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 "   /> < title > 无标题文档 </ title > </ head > < body > <% userId = Request.QueryString( " userid " )set rs = server.CreateObject( " adodb.recordset " )sqlcmd = " select top 1* from oa_user_photo where PhotoTypeId=1 and UserId= " & userId & " order by Id ASC " rs.open sqlcmd,conn, 1 , 1 path = replace(rs( " PhotoPath " ), " photo/ " , " ")f2 = server.MapPath( " .. / photo " )&pathResponse.Write( " < img src = ' "&f2&" '  width = ' 150 '  height = ' 200 ' > " )rs.closeconn.close%></body></html>

     

     


    最新回复(0)