读书人

ajax有关问题,无法获取数据

发布时间: 2011-12-23 23:32:01 作者: rapoo

ajax问题,无法获取数据
我新建了一个查询页(Select.aspx)和一个获取查询结果的页(GetSelectData.aspx)
下面是select.aspx的js代码:

JScript code
var xmlhttp;function GetData(){  var time=document.getElementById("<%=TimeTB.ClientID%>").value;  var scity=document.getElementById("<%=SCityTB.ClientID%>").value;  var dcity=document.getElementById("<%=DCityBT.ClientID%>").value;  if(window.ActiveXObject)  {   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器  }  else if(window.XMLHttpRequest)//非IE浏览器  {   xmlhttp=new XMLHttpRequest();  }  else  {    alert("创建XMLHttpRequest对象失败!");    return;  }  xmlhttp.Open("GET","GetSelectData.aspx?stime="+time+"&scity="+scity+"&dcity="+dcity,false);  xmlhttp.onreadystatechange=handlfunc;  xmlhttp.send(null);}function handlfunc()//获取数据后处理函数{   if(xmlhttp.readystate==4)   {      if(xmlhttp.status==200)      {      var data=bytes2BSTR(xmlhttp.ResponseBody);      var start=data.indexOf("查找结果");      var end=data.indexOf("结束");      var finaldata=data.substring(start+8,end);      document.write(xmlhttp.ResponseText);      //document.getElementById('data_div').innerHTML=data;      //document.getElementById("data_div").innerHTML=xmlhttp.ResponseText;      }   }  

GetSelectData.aspx页面的后台代码:
C# code
 protected void Page_Load(object sender, EventArgs e)    {        string time = "2009-08-05",scity="南昌",dcity="景德镇";        if (!IsPostBack)        {            try            {                time = Request.QueryString["stime"].ToString();                scity = Request.QueryString["scity"].ToString();                dcity = Request.QueryString["dcity"].ToString();            }            catch (Exception ex)            {                Response.Redirect("ErrorPage.htm");            }            LoadData(time, scity, dcity);        }            }    public void LoadData(string time,string scity,string dcity)    {        SqlParameter[] sp ={DBAccess.MakeParam("@stime",SqlDbType.VarChar,time),                            DBAccess.MakeParam("@scity",SqlDbType.VarChar,scity),                            DBAccess.MakeParam("@spoint",SqlDbType.VarChar,""),                            DBAccess.MakeParam("@dcity",SqlDbType.VarChar,dcity),                            DBAccess.MakeParam("@dpoint",SqlDbType.VarChar,"")                            };        int flag = 0;        DataView dv = DBAccess.GetData("qiche_SelectBusInfo", sp, out flag).Tables[0].DefaultView;        if (flag == 11)        {            Response.Redirect("ErrorPage.htm");        }        this.DataList1.DataSource = dv;        this.DataList1.DataBind();     }

现在问题,当输入查询条件时,无查询到数据,得到是GetSelectData.aspx页面没绑定数据时的东西.
还有就当用responsebody时,出现乱码,如何解决。
小弟,刚开始弄这个ajax这个东东。有很多不懂,现在是一头雾水。

[解决办法]
解决方法:
在GetSelectData.aspx页面的后台重写Render方法和VerifyRenderingInServerForm方法

C# code
 protected override void Render(HtmlTextWriter writer)        {            HtmlTextWriter gridWriter = new HtmlTextWriter(new System.IO.StringWriter());            DataList1.RenderControl(gridWriter);            //给ajax返回数据            Response.Write(gridWriter.InnerWriter.ToString());        }        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)        {        }
------解决方案--------------------


C# code
//在方法最後加  StringBuilder sb = new StringBuilder();  StringWriter stWriter = new StringWriter(sb);  HtmlTextWriter htmlWriter = new HtmlTextWriter(stWriter);  DataList1.RenderControl(htmlWriter);  Response.wirte(sb.ToString());//重VerifyRenderingInServerFormpublic override void VerifyRenderingInServerForm(Control control){}
[解决办法]
探讨
引用:
都什么和什么 ~

你始终获得, 得到是GetSelectData.aspx页面没绑定数据时的东西
是因为 缓存的问题
你在 get 请求中 加上时间戳 就可以了
JScript code
xmlhttp.Open("GET","GetSelectData.aspx?stime="+time+"&scity="+scity+"&dcity="+dcity+"&date="+new Date(),false);


加了时间戳后台要处理不?

[解决办法]
你用断点跟踪下GetSelectData.aspx 获得的条件
你用Get方式传过去的中文有乱码出现,所以你的查询条件不成立,所以查不到数据
试试看

[解决办法]
什么乱七八糟的

42楼说的很明白了,你先要检查参数是否正常的传递过去,并且保存查询有数据
如果你不确定,直接输出Reqeust.QueryString的数据出来看看
Response.CharSet = "gb2312 ",这样XMLHTTP接收到的才不会是乱码

你这个传值方式有待讨论,最好是使用post方式,使用get的确会出现时间问题

另外你可以使用


xmlhttp.Open( "GET ", "GetSelectData.aspx?stime= "+escape(time)+ "&scity= "+escape(scity)+ "&dcity= "+dcity+ "&date= "+(new Date()),false);

一,进行了转义,假如你的scity中有一个字符为 "& "不乱了?
二,防止了乱码.


[解决办法]
你传的是中文?
那要先在JavaScript中编码然后传过去.
如: xmlhttp.Open("GET","GetSelectData.aspx?stime="+escape(time)+"&scity="+escape(scity)+"&dcity="+escape(dcity),false);

接收后需要解码

[解决办法]
中文要用 escape 编码后传递
JScript code
xmlhttp.Open("GET","GetSelectData.aspx?stime="+escape(time)+"&scity="+escape(scity)+"&dcity="+escape(dcity)+"&date="+new Date(),false);
[解决办法]
GetSelectData.aspx
HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetSelectData.aspx.cs" Inherits="csdn_GetSelectData" %><!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 runat="server">    <title>无标题页</title></head><body>    <form id="form1" runat="server">    <div>    查找结果:      <asp:DataList id="DataList1" runat="server" FooterStyle-BorderStyle="None" ItemStyle-BorderStyle="None" BorderStyle="None" CellPadding="0" CellSpacing="0" BorderWidth="0"  Width="100%">                <HeaderTemplate>                   <table width="100%" cellpadding="0" cellspacing="0">                     <tr style=" background-color:Fuchsia">                       <td>                       发车城市-地点                       </td>                       <td>                       目的城市-地点                       </td>                       <td>                       发车时间                       </td>                       <td>                        剩余可售座位数                       </td>                       <td>                        散客价(元/人)                       </td>                       <td>                       团体价(元/人)                       </td>                       <td>                       成团人数                       </td>                       <td>                                             </td>                     </tr>                </HeaderTemplate>                <ItemTemplate>                     <tr onmouseover=" ChangTrBg(this,0)" onmouseout="ChangTrBg(this,1)">                                                 <td>                       <%# DataBinder.Eval(Container.DataItem,"StartCity")+"-"+DataBinder.Eval(Container.DataItem,"StartPoint")  %>                       </td>                       <td>                       <%# DataBinder.Eval(Container.DataItem,"DestionCity")+"-"+DataBinder.Eval(Container.DataItem,"DestionPoint") %>                       </td>                       <td>                       <%# DataBinder.Eval(Container.DataItem, "StartTime") == "" ? DataBinder.Eval(Container.DataItem, "UnSureTime") == "" ? DataBinder.Eval(Container.DataItem, "TimeDesp") : DataBinder.Eval(Container.DataItem, "UnSureTime") : DataBinder.Eval(Container.DataItem, "StartTime")%>                       </td>                       <td>                       <%# DataBinder.Eval(Container.DataItem,"RestNumber") %>                       </td>                       <td>                       <%# DataBinder.Eval(Container.DataItem,"SiglePrice")%>                       </td>                       <td>                       <%# DataBinder.Eval(Container.DataItem,"TeamPrice") %>                       </td>                       <td>                       <%# DataBinder.Eval(Container.DataItem,"TeamNumber") %>                       </td>                       <td>                       <a href='DetailCarInfo.aspx?id=<%#DataBinder.Eval(Container.DataItem,"OrderId") %>'>更多>></a>                       </td>                    </tr>                </ItemTemplate>                <FooterTemplate>                  </table>                </FooterTemplate>                  <FooterStyle BorderStyle="None" BorderWidth="0px"  />                  <ItemStyle BorderWidth="0px"  />                  <HeaderStyle BorderStyle="None" BorderWidth="0px"  />              </asp:DataList>       结束    </div>    </form></body></html> 

读书人网 >asp.net

热点推荐