ashx 文件的使用

它就类似.aspx文件,用于处理传入到服务器的HTTP请求,但它不会像.aspx文件那样要返回处理结果和大量HTML,它可以返回简单的字符串、图片等。

百度百科定义链接:http://baike.baidu.com/view/3799515.htm

开发实例如下:

前端请求核心代码(json):

 var jsonArray= new Array();        

  var jsonObj = {};          

       jsonObj["CourseId"] = $(this).children().eq(0).text();;          

       jsonObj["CertCategory"] = $(this).children().eq(3).text();              

      jsonObj["SubCertCategory"] = $(this).children().eq(4).text();                

      jsonArray.push(jsonObj)         

    });         

    var jsonDate =               

  {                   

             jsontype: "objType",                  

             jsonCount: jsonArray.length,               

             jsonArray: jsonArray

  }           

  $.ajax({                  

   type: "post",                  

   url: "/AjaxPage/Certification.ashx",    

    dataType: "json",             

    data: objDate,         

     complete :function(){$("#load").hide();},//AJAX请求完成时隐藏     

    success: function (data)

    {                   

        if (data["Succeed"])

       {                          

             alert("提交成功!");                           

             window.location.href = “";                       

        }                    

      },             

        error: function (XMLHttpRequest, textStatus, errorThrown)

       {

           alert("请求对象XMLHttpRequest: "+XMLHttpRequest);
           alert("错误类型textStatus: "+textStatus);
           alert("异常对象errorThrown: "+errorThrown);          

        }                

});           

后台ashx 接收:

  public void ProcessRequest(HttpContext context)        

  {                 

        string jsontype= context.Request["jsontype"];           

       int  certCount=context.Request["jsonCount"];  

        AjaxResult    result = new AjaxResult ();         

        switch (jsontype)            

        {                

                 case "objType":                    

                {                        

                     #region 操作        

                  for (int k = 0; k < certCount; k++)
                  {
                        var courseId = context.Request["jsonArray[" + k + "][CourseId]"];   

                  }                               

                    result.Succeed = true;                            

                    result.resultMsg= ex.Message;       

                    context.Response.Write(JsonConvert.SerializeObject(result));                                             

                  }

                break;  

                #endregion                                  

           }                  

 }

返回信息构建:

    public class AjaxResult     {

      public bool Succeed { get; set; }       

      public string resultMsg { get; set; }       

      public object  ObjInfo { get; set; }        

      public void setTrue(string message)       

     {            

            this.Succeed = true;         

             this.resultMsg= message;        

      }

        public void setError(string message)        

        {          

                this.Succeed = false;           

                this.resultMsg= message;    

         }

    }

 

posted @ 2016-02-29 17:07  李寒星  阅读(1342)  评论(0编辑  收藏  举报