日期:2014-05-17  浏览次数:20450 次

JS获取Response.Write乱码~!
JS 模拟AJAX:(代码不用细看,只需看注释即可)
JScript code
<script language="javascript">
        var httpxml;
        function GetAllBook()
        {
            var pageIndex = document.getElementById("hfPageIndex").value;
            var condition = document.getElementById("txtCondition").value;
            httpxml = new ActiveXObject("Microsoft.XMLHTTP");
            httpxml.open("Post","Ajax.aspx?Type=GetOrder&pageIndex="+pageIndex+"&condition="+condition);
            httpxml.onreadystatechange = OnMessageBack;
            httpxml.send(null);
        }
        
        function OnMessageBack()
        {
            if(httpxml.readystate == 4 && httpxml.status == 200)
            {
                if (httpxml.responsetext !="")
                {
                    var tab = document.getElementById("tabOrder");
                    AddRows(tab,20 ,5,httpxml.responsetext);
                }
            }
        }
    </script>


C# code
namespace Reader
{
    public partial class Ajax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Type"] != null)
            {
                string type = Request.QueryString["Type"].Trim();

                if (type.Equals("GetOrder"))
                {
                    int index = int.Parse(Request.QueryString["pageIndex"].Trim());
                    string condition = Request.QueryString["condition"].Trim();
                    string sql = "";
                    if (condition == "")
                    {
                        sql = string.Format("SELECT * FROM BookInfo ORDER BY bookName ASC LIMIT {0},100;", (index - 1) * 100);
                    }
                    else
                    {
                        sql = string.Format("SELECT * FROM BookInfo WHERE bookName = '{0}' ORDER BY bookName ASC;", condition);
                    }
                    string returnString = "";
                    DataTable dt = MySQL.GetDataSet(sql).Tables[0];
                    foreach (DataRow row in dt.Rows)
                    {
                        returnString += row["bookName"].ToString().Trim() + "□";
                        //returnString += Server.UrlEncode(row["path"].ToString().Trim()) + "■";
                        returnString += row["path"].ToString().Trim() + "■";
                    }
                    Response.Clear();
                    Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                    Response.Write(returnString.TrimEnd('■'));//此处回传数据
                    Response.End();
                }
            }
        }
    }
}


JScript code
function AddRows(tab,rowCount,columCount,sourec)
{
    var items;
    if(sourec != "")
    {
        items = sourec.split('■');
    }
    for(var i = 0;i < rowCount;i++)
    {
        var newRow = tab.insertRow();
        if(i%2 == 0)
            newRow.style.background = "Pink";
        else
            newRow.style.background = "Green";
            
        for(var j = 0;j < columCount;j++)
        {
            var newCell = newRow.insertCell();
            var bName = "";
            var path = "";
            if(i*5+j < items.length)