Ajax 普通应用

Ajax通用代码

View Code
//xmlhttpRequest的创建
        var xmlhttp;
        function CreateXmlHttp() {
            xmlhttp = null;
            if (window.XMLHttpRequest) {// code for all new browsers
                xmlhttp = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) {// code for IE5 and IE6
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

        }
//控件调用的方法(以根据号码获取客户信息为例)
function 方法名称(obj) {
            if (window.event.keyCode == 13) {
                objValue = obj.value.replace(/(^\s*)|(\s*$)/g, "");
                if (objValue.length == 0)
                { document.getElementById("<%=TxtFhLxr.ClientID %>").focus();return; }
                else {
                    //ajax通过电话获取信息
                    CreateXmlHttp();
                    if (xmlhttp != null) {
                        xmlhttp.onreadystatechange = GetFkhstate_Change;
                        xmlhttp.open("GET", "/aspx/ajax.ashx?t=" + Date.toString() + "&type=getkh&value=" + objValue, true);
                        xmlhttp.send(null);
                    }
                    else {
                        alert("Your browser does not support XMLHTTP1.");
                    }
                }
                document.getElementById("<%=TxtFhLxr.ClientID %>").focus();
            }
        }
        //对返回结果进行解析
        function GetFkhstate_Change() {
            if (xmlhttp.readyState == 4) {// 4 = "loaded"
                if (xmlhttp.status == 200) {// 200 = OK
                    var value = xmlhttp.responseText;
                    if (value != "nocount") {
                        var arr = value.split(","); //khmc,lxr,area
                        document.getElementById("<%=TxtFhDw.ClientID%>").value = arr[0];
                        document.getElementById("<%=TxtFhLxr.ClientID%>").value = arr[1];
                        document.getElementById("<%=TxtFhAdder.ClientID%>").value = arr[2];

                    }
                }
                else {
                    alert("Problem retrieving XML data");
                }
            }
        }

下面是Ajax.ashx(在Ajax中调用)

View Code
string conStr = ConfigurationManager.ConnectionStrings["字符串"].ToString();
            //context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/UTF-8";
            //context.Response.Write("Hello World");
            string type = context.Request.QueryString["type"].ToString().ToLower();
            string value = context.Request.QueryString["value"].ToString().ToLower();
  

                    DataRow dr = ClsRWMSSQLDb.GetDataRow(
                        string.Format("select id,mc,lxr,tel,address,yhid,yhzh from Tkh where bh = '{0}' and lx = 'A' and zt=1", value), conStr);
                    if (dr == null)
                    {
                        context.Response.Write("nocount");
                    }
                    else
                    {
                        string values = "";
                        values += dr["id"].ToString() + ",";
                        values += dr["mc"].ToString() + ",";
                        values += dr["lxr"].ToString() + ",";
                        values += dr["tel"].ToString() + ",";
                        values += dr["address"].ToString();
                        context.Response.Write(values);
                    }
                            }

 

posted @ 2013-05-13 09:59  L嗜血幽灵  阅读(101)  评论(0)    收藏  举报