代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
ASP.NET 简单的 JSON 处理实现 Ajax 功能
作者:
Dezai.CN
/ 发布于
2011/6/15
/
553
<div>[JavaScript]代码</div> <div>var ajax = {</div> <div> setDebugMode: function(){ this.debugMode = true; },</div> <div> get: function (requestUrl, data, loaderImageId, onSuccess, onComplete) {</div> <div> if (loaderImageId) { $("#" + loaderImageId).css('display', 'inline'); } <div> var completeFunction = function () { if (onComplete) { onComplete(); } if (loaderImageId) { $("#" + loaderImageId).css('display', 'none'); } };</div> <div> $.ajax({ type: "GET", url: requestUrl, data: data, context: this, success: function (response) { onSuccess(response); completeFunction(); }, error: function (response) { alert("Ajax Request Failed"); this.showError(response); completeFunction(); } }); },</div> <div> post: function (requestUrl, data, loaderImageId, onSuccess, onComplete) {</div> <div> if (loaderImageId) { $("#" + loaderImageId).css('display', 'inline'); } <div> var completeFunction = function () { if (onComplete) { onComplete(); } if (loaderImageId) { $("#" + loaderImageId).css('display', 'none'); } };</div> <div> $.ajax({ type: "POST", url: requestUrl, context: this, data: data, success: function (response) { onSuccess(response); completeFunction(); }, error: function (response) { alert("Ajax Request Failed"); this.showError(response); completeFunction(); } }); },</div> <div> showError: function(requestObject){ if (this.debugMode){ alert(requestObject.responseText); } } };</div> <div>JsonRequestHandler.cs</div> <div>using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Reflection; using System.Web.SessionState;</div> <div>namespace SampleJSONWeb { public abstract class JsonRequestHandler : IHttpHandler, IRequiresSessionState {</div> <div> public abstract void ProcessRequest(HttpContext context);</div> <div> protected void SetResponse(HttpContext context, string jsonResponse) { context.Response.Cache.SetExpires(DateTime.Now); context.Response.ContentType = "application/json"; context.Response.Write(jsonResponse); context.Response.End(); } <div> protected void SetResponse(HttpContext context, Dictionary<string, string> attributes) { SetResponse(context, GetJsonObject(attributes)); } <div> protected void SetResponse<T>(HttpContext context, T obj) { SetResponse(context, GetJsonObject<T>(obj)); } <div> protected T CreateObjectFromRequest<T>(HttpContext context) where T: new() { T ob = new T(); PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo property in properties) { string value = context.Request.Params[property.Name]; if (value == null) { continue; } <div> if (property.PropertyType != typeof(string) && value == string.Empty) { continue; } <div> object convertedValue = Convert.ChangeType(value, property.PropertyType); if (convertedValue == null) { continue; } <div> property.SetValue(ob, convertedValue, null); } return ob; } <div> protected string GetJsonObject(Dictionary<string, string> attributes) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{"); bool firstTime = true; foreach (string key in attributes.Keys) { if (!firstTime) { jsonBuilder.Append(","); } <div> string name = key; object value = attributes[key]; jsonBuilder.Append("\"" + name + "\":" + value.ToString()); firstTime = false; } jsonBuilder.Append("}"); return jsonBuilder.ToString(); } <div> protected string GetJsonCollection<T>(IEnumerable<T> collection) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("[");</div> <div> bool first = true; foreach (T item in collection) { if (!first) { jsonBuilder.Append(","); } jsonBuilder.Append(GetJsonObject<T>(item)); first = false; } <div> jsonBuilder.Append("]"); return jsonBuilder.ToString(); } <div> protected string GetJsonObject<T>(T obj) { PropertyInfo[] properties = typeof(T).GetProperties(); StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{"); bool firstTime = true; foreach (PropertyInfo property in properties) { if (!firstTime) { jsonBuilder.Append(","); } <div> string name = property.Name; object value = property.GetValue(obj, null); jsonBuilder.Append("\"" + name + "\":\"" + value.ToString() + "\""); firstTime = false; } jsonBuilder.Append("}"); return jsonBuilder.ToString(); } <div> public bool IsReusable { get { return false; } } } } <div></div>
试试其它关键字
JSON
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3