时间:2021-07-01 10:21:17 帮助过:4人阅读
                            } 
                        $("#divWait").hide(); 
                        $("#btnPost").attr("disabled", ""); 
                    }); 
}
该方法参数也很多,具体可看帮助文档。本人的常规用法
                }); 
        }
context.Response.ContentType = "application/json";
如果是返回的html或者text的话可以如下写法
context.Response.ContentType = "text/plain";
如果ajax方法中设置的返回值是json时,ashx代码返回的格式必须是json格式的数据。
把一个对象转换成json格式,常用方法就是采用开源的第三方类库json.net,Newtonsoft.Json.dll.
JsonConvert.SerializeObject方法就可以转换了。返回json格式后,jquery就可以采用XXX.xxx的方式获取值了。
JsonConvert在处理datetime格式的时候,会返回类似1198908717056的绝对值,因此,在处理datetime的时候,要做一下转换。具体语句如下:
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();           
//这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式            
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"; 
string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);
此处顺便提一下,javascript对json格式的数据有着天生的处理能力,非常好的兼容json格式数据。
举个例子:
ashx完整代码如下:
    namespace nnn
{ 
    /// 
    /// PostIt 的摘要说明 
    /// 
    public class PostIt : IHttpHandler 
    {
            public void ProcessRequest(HttpContext context) 
        { 
            context.Response.ContentType = "application/json"; 
            try 
            { 
                string msgContent = context.Request["msgContent"] ?? ""; 
                ModelContent m = new ModelContent() 
                { 
                    author = "", 
                    categoryid = -1, 
                    title = "", 
                    content = msgContent, 
                    datetime = DateTime.Now, 
                    key = "", 
                    createdate = DateTime.Now, 
                    lastmodifydate = DateTime.Now, 
                    ip = context.Request.UserHostAddress
};
                    //BLLContent bll = new BLLContent(); 
                //bll.Add(m);
                    IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();          
                //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式           
                timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"; 
                string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter); 
                context.Response.Write(output); 
            } 
            catch (Exception ex) 
            { 
                context.Response.Write(ex.Message); 
            }
}
            public bool IsReusable 
        { 
            get 
            { 
                return false; 
            } 
        } 
    } 
}