Jquery 请求ashx

 做了一个文章列表,要实现增删改查功能,以前常用用ajax方法中的(ajaxpro),jquery也行,那就用jquery吧,小试一下牛刀

以前做数据删除的时候都有去aspx页请求的,后来知道用ashx会更快些,以下是方法

jquery方法

 

代码
function buildxml()
     {
      
             $.ajax({
                type: 
"get",
                url: 
"/db/AjaxHelp.ashx",
                data:
'buildxml=1',
                beforeSend: function(XMLHttpRequest){             
                  
                  
                    
//ShowLoading();
                },
                success: function(data, textStatus){               
                  alert(
'OK'); alert(data);
                },
                complete: function(XMLHttpRequest, textStatus){
                    
//HideLoading();
                },
                error: function(){
                    alert(
'error');
                }
                    });

 

 

 

 

以下是ashx方法

代码
  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType 
= "text/plain";
           
// context.Response.Write("Hello World");
            string buildxml =context.Request.QueryString["buildxml"].ToString();
            
if (buildxml == "1")
            {
                
if (CreateXML())
                {
                    context.Response.Write(
"生成成功!");
                }
                
else
                {
                    context.Response.Write(
"生成失败!");
                }
            }
        }



        
private bool CreateXML()
        {
            
try
            {

                DB.DBHelperDataContext bll 
= new DBHelperDataContext();
                var q 
= (from s in bll.IndexNews
                         
where s.Types==1
                         orderby s.ID descending
                         select 
new{s.ID,s.Title}).Take(3);
               

                
string topNews = System.Configuration.ConfigurationManager.AppSettings["topNews"].ToString();

              
//  DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.SSCSConnectString, CommandType.Text, sqlStr);




                
int i=0;

               
foreach(var m in q)
               {
                  
                   
string id = m.ID.ToString();
                   
string name = m.Title.Length>18?m.Title.Substring(0,18):m.Title;
                  
                    
if (i == 0)
                    {
                        XmlParameter[] param 
= { new XmlParameter("id", id), new XmlParameter("name", name) };
                        XMLHelper.CreateXMLFile(topNews, 
new XmlParameter("books"), "chapter", param);
                    }
                    
else
                    {
                        XmlParameter[] aparam 
= { new XmlParameter("id", id), new XmlParameter("name", name) };
                        XMLHelper.AddNewNode(topNews, 
"books", aparam);

                    }
                    i
++;
                }
             
            }
            
catch (Exception ex) { PageHelper.Alert(ex.ToString()); return false; }
            
return true;

        }

 

 

posted @ 2010-09-21 17:31  咸鱼公子  Views(1515)  Comments(1Edit  收藏  举报