jquery 异步请求

1、js代码

 $.ajax({
                async: true, // 默认true(异步请求)
                cache: false, // 默认true,设置为 false 将不会从浏览器缓存中加载请求信息。
                type: "POST", // 默认:GET 请求方式:[POST/GET]
                dataType: "xml", //默认["xml"/"html"] 返回数据类型:["xml" / "html" / "script" / "json" / "jsonp"]
                url: "/Manage/Handler/ChannelHandler.ashx?type="+type+"&Group="+GroupId, // 默认当前地址,发送请求的地址
                error: function(xml) {  window.location="/Manage/Index.aspx"; }, // 请求失败时调用
                timeout: 100000, // 设置请求超时时间
                success: function(xml) { // 请求成功后回调函数 参数:服务器返回数据,数据格式.
                    // 用Jquery处理xml数据
                     $(xml).find("GroupChannel").each(function(){
                      var $GroupChannel = $(this);   
                      var GroupId = $GroupChannel.find('GroupId').text();  
                      var ChannelId = $GroupChannel.find('ChannelId').text();
                      var ChannelEName = $GroupChannel.find('ChannelEName').text();
                      var ChannelCName = $GroupChannel.find('ChannelCName').text();
                      var ChannelUrl = $GroupChannel.find('ChannelUrl').text();
                      var ChannelTarget = $GroupChannel.find('ChannelTarget').text();
                      var ChannelIsSys = $GroupChannel.find('ChannelIsSys').text(); 
                      var ChannelParentId = $GroupChannel.find('ChannelParentId').text(); 
                      var ChannelDepthId = $GroupChannel.find('ChannelDepthId').text(); 
                      var ChannelOrder = $GroupChannel.find('ChannelOrder').text();  
                      var html = '<li style="list-style:none;padding-left:5px;"><img src="Content/Blue/Images/menu_dot_21.gif" alt="" border="0"><a class="menulist" href=' + ChannelUrl + ' target=' + ChannelTarget + '>' + ChannelCName + '</a></li>';      
                      
                      $("#menuNavi_id").append($(html));            
                     });
                }
            });
View Code

asxh 代码

            int ChannelParentId = int.Parse(context.Request["type"].ToString());
            int GroupId = int.Parse(context.Request["Group"].ToString());
            context.Response.ContentType = "text/xml";
            context.Response.Charset = "utf-8";
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
            xml += "<GroupChannels>";
            IList<ChannelInfo> items = ChannelBLL.GetChannelList(""," where ChannelParentId=" + ChannelParentId + " and ChannelId in (select ChannelId from bz_GroupChannel where GroupId=" + GroupId + ") order by ChannelOrder");
            foreach (ChannelInfo item in items)
            {
                xml += "<GroupChannel>";
                xml += "<ChannelId>" + item.ChannelId + "</ChannelId>";
                xml += "<ChannelEName>" + item.ChannelEName + "</ChannelEName>";
                xml += "<ChannelCName>" + item.ChannelCName + "</ChannelCName>";
                xml += "<ChannelIco>" + item.ChannelIco + "</ChannelIco>";
                xml += "<ChannelUrl>" + item.ChannelUrl + "</ChannelUrl>";
                xml += "<ChannelTarget>" + item.ChannelTarget + "</ChannelTarget>";
                xml += "<ChannelIsSys>" + item.ChannelIsSys + "</ChannelIsSys>";
                xml += "<ChannelParentId>" + item.ChannelParentId + "</ChannelParentId>";
                xml += "<ChannelDepthId>" + item.ChannelDepth + "</ChannelDepthId>";
                xml += "<ChannelOrder>" + item.ChannelOrder + "</ChannelOrder>";
                xml += "</GroupChannel>";
            }

            xml += "</GroupChannels>";

            context.Response.Write(xml);
View Code


2.js代码

  //获取图书列表并绑定到booklist
        function GetBookList(pageIndex) {
            $.ajaxSetup({ async: false });
            $.ajaxSetup({ cache: true });
            $.ajaxSetup({ error: function(data) { alert("请求超时!") } });
            $.ajaxSetup({ timeout: 180000});
            $.getJSON('/SearchResult/handler/CoffeeHandler.ashx?callback=?', { PageIndex: pageIndex, PageSize: pageSize, keys: decodeURI(GetQueryString("CoffeeName")) }, function(data) {
                $(".bo").empty();

                $(".bo").remove();
                if (iBookCount == 0) {
                    $("#showBook").hide();
                    $("#tishi").show();
                }

                $("#userTemplate").render(data).appendTo("#bookList");
            });
          
        }
View Code

asxh

 if (!string.IsNullOrEmpty(context.Request["PageIndex"]))
            {
                string keys = context.Request["keys"];
                string strwhere = string.Format(" Type=4 and (Name like '{0}%'or KeyWords like '%{1}%') ", keys, keys );
                int iPageIndex = int.Parse(context.Request["PageIndex"]);
                PageListInfo pli = new PageListInfo();//分页对象
                pli.tblName = "bz_ZhuangYuanInfo";
                pli.fldName = @"Logo,Name,Address,Tel,ZhuangYuanInfoId,Type";
                pli.fldSort = "ZhuangYuanInfoId";
                pli.KeyID = "ZhuangYuanInfoId";
                pli.Sort = false;
                pli.strWhere = "and" + strwhere;//查询where语句
                pli.currentPageIndex = iPageIndex;
                pli.PageSize = int.Parse(context.Request["PageSize"]);
                PageListInfo plii = PageListBLL.GetPageList(pli);
                JavaScriptSerializer json = new JavaScriptSerializer();

                IList<Dictionary<string, string>> items = new List<Dictionary<string, string>>();

                foreach (DataRow dr in plii.ReturnTable.Rows)
                {
                    Dictionary<string, string> item = new Dictionary<string, string>();
                    item.Add("Logo", dr["Logo"].ToString());
                    item.Add("Name", dr["Name"].ToString());
                    item.Add("Address", dr["Address"].ToString());
                    item.Add("Tel", dr["Tel"].ToString());
                    item.Add("ZhuangYuanInfoId", dr["ZhuangYuanInfoId"].ToString());
                    items.Add(item);
                }

              //  sResult = json.Serialize(items);
                sResult = context.Request["callback"] + "(" + json.Serialize(items) + ")";
            }


            context.Response.Write(sResult);
            context.Response.End();
         
View Code
原文地址:https://www.cnblogs.com/nik2011/p/3079379.html