手写ajax读取数据例子

< script >

function  objAjax()
{
var  objxml = false ;
  
// 尝试创建Microsoft IE浏览器支持的对象
  try  {
objxml 
=   new  ActiveXObject( " Msxml2.XMLHTTP " );
catch  (e) {
try  {
objxml 
=   new  ActiveXObject( " Microsoft.XMLHTTP " );
catch  (e) {
aobjxml 
=   false ;
}
}
  
// 如果创建不成功,则尝试创建非Microsoft IE浏览器支持的对象
  if  ( ! objxml  &&   typeof  XMLHttpRequest != ' undefined ' ) {
objxml 
=   new  XMLHttpRequest();
}
return  objxml;
}

 


function  openurl(url)
{
   
var  objxml = objAjax();
  
//   var objxml=new ActiveXObject("Microsoft.XMLHttp") //建立

    objxml.open(
" GET " ,url, false );  // 打开
    objxml.send();  // 发送
    retInfo = objxml.responseText; 接收 返回文本字符串
   
 
if (objxml.readyState  ==   1 )   // 初始化
       {
            document.getElementById(
" aboutproDiv " ).innerHTML  = " 正在加载数据... " ;
        }

    
if (objxml.readyState  ==   4 )   // 数据接收完毕
            { 
               document.getElementById(
" aboutproDiv " ).style.display = " none " ; // 隐藏
                 return  retInfo;
    }
    
else
    {
    
return   " -2 " ;
    }
}

function  checkapp(form){
    infoBoard
= document.getElementById( " aboutpro_i " );
    myurl
= " getdis.aspx?pid= " +<!-- @proId@ -->+ " &rnd= " + Math.random();
    
var  retCode  =  openurl(myurl);
    
switch (retCode){
        
case   " -2 " : infoBoard.innerHTML = ' 该商品暂无评论 ' ; break ;
         
default :{
                  infoBoard.innerHTML
= retCode;              
                 }
break ;
    }
    
return
}

 
< / script>

 


< body onload = " checkapp() " >

 

 
< div id = " aboutpro_i " >
< DIV id = aboutproDiv  > 正在加载中... </ DIV >
  
<!-- @item@ -->
               
</ div >

 

// getdis.aspx  cs代码

  
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  (Request[ " pid " !=   null )
        {
            
string  strReturn  =   " 该商品暂无评论... " ;
            
string  strSql  =   " select * from cuspro where pid= " + Request[ " pid " ].ToString() + "  order by id desc " ;
            chat c 
=   new  chat();
            DataTable dt 
=  c.SqlselectPro(strSql, 2 );
            
if  (dt.Rows.Count  >   0 )
            {
                strReturn 
=   "" ;
            
              
foreach (DataRow dr  in  dt.Rows)
              {
                  
string  re = dr[ " reply " ].ToString();
                  
if  (re  !=   "" )
                  {
                      strReturn 
+=   " <div class='protitle'>评论者: "   +  dr[ " mname " ].ToString()  +   "      "   +  dr[ " ctime " +   " &nbsp;&nbsp;对  "   +  dr[ " pname " ].ToString()  +   "  评论!</div> " ; ;
                   
                      strReturn 
+=   " <div class='aboutproitem'> "   +  dr[ " items " ].ToString()  +   " </div> " ;
                      strReturn 
+=   " <div class='aboutproitem1'> "   +  dr[ " reply " ].ToString()  +   " </div> " ;
                  }
                  
else
                  {

                      strReturn 
+=   " <div class='protitle'>评论者: "   +  dr[ " mname " ].ToString()  +   "      "   +  dr[ " ctime " +   " &nbsp;&nbsp;对  "   +  dr[ " pname " ].ToString()  +   "  评论!</div> " ; ;
                 
                      strReturn 
+=   " <div class='aboutproitem'> "   +  dr[ " items " ].ToString()  +   " </div> " ;
                  }
              }
            }
            Response.Write(strReturn);
        }
    }

你可能感兴趣的:(Ajax)