标签:http java os io for cti ar javascript
调用 var b = $ajax("WebApplication1._Default.GetDataSet", ["23"]);
js:/*方法全名,参数,类所在的程序集名称(当方法类所在的命名空间不是程序集名称时请传参数来)*/
function $ajax(paramMethod, paramValue, paramAssemblyName) {
if (!paramMethod) { Dialog.alert("缺少全名称的方法参数", 1); }
var result, params = "", assemblyName, className, methodName;
if (paramValue) for (var i = 0; i < paramValue.length; i++) { params += "¶m" + i + "=" + encodeURI(paramValue[i]); }; //获取参数
className = paramMethod.substring(0, paramMethod.lastIndexOf(‘.‘)); // 获取方法所在类名称
methodName = paramMethod.substring(paramMethod.lastIndexOf(‘.‘) + 1); // 获取方法名称
assemblyName = className.substring(0, className.lastIndexOf(‘.‘)); if (paramAssemblyName) assemblyName = paramAssemblyName; // 获取命名空间名称
$.ajax({ url: "/../Ajax.ashx?Assembly=" + assemblyName + "&Class=" + className + "&Method=" + methodName + params, type: "POST", async: false,
success: function (data) { result = eval("(" + data + ")"); },
error: function (data) {
layer.msg(data.responseText, 2, 0);
}
});
return result;
}
Ajax.ashx:
using System; using System.Collections.Generic; using System.Web; using System.Web.SessionState; using System.Data; using System.Reflection; using System.IO; using System.Web.Script.Serialization;
namespace Chengrui.RSP.UILayer { /// <summary> /// Ajax 的摘要说明 /// </summary> public class Ajax : IHttpHandler, IRequiresSessionState {
#region public void GetMethod() 获取参数 /// <summary> /// 获取参数 /// </summary> /// <param name="context">界面内容</param> /// <param name="paramField">参数</param> /// <returns></returns> public string GetRequest(HttpContext context, string paramField) { try { return context.Request[paramField] == null || context.Request[paramField].ToString() == "undefined" ? "" : context.Request[paramField].ToString().Trim(); } catch { return string.Empty; } } #endregion
public void ProcessRequest(HttpContext context) { object result = null; try { string assemblyName = this.GetRequest(context, "Assembly"), className = this.GetRequest(context, "Class"), methodName = this.GetRequest(context, "Method"); //获取程序集 Type type = null; try { Assembly myAssembly = Assembly.Load(assemblyName); type = myAssembly.GetType(className); } catch { type = Assembly.GetExecutingAssembly().GetType(className); }
//找不到程序集,就遍历基目录下的所有以Chengrui.RS.命名的程序集,而获取方法 if (type == null) { FileInfo[] arrFileInfo = new DirectoryInfo(AppDomain.CurrentDomain.RelativeSearchPath).GetFiles("Chengrui.RS.*.dll"); foreach (FileInfo myFile in arrFileInfo) { type = Assembly.Load(myFile.Name.Substring(0, myFile.Name.LastIndexOf(‘.‘))).GetType(className); if (type != null) break; } }
if (type != null) { System.Reflection.MethodInfo methodInfo = type.GetMethod(methodName); if (methodInfo != null)//判断操作页面是否有该方法 { object[] args = new object[methodInfo.GetParameters().Length]; //对方法名的参数进行赋值 for (int i = 0; i < methodInfo.GetParameters().Length; i++) { args[i] = Convert.ChangeType(this.GetRequest(context, "param" + i.ToString()), methodInfo.GetParameters()[i].ParameterType); } //执行方法 result = methodInfo.Invoke(Activator.CreateInstance(type), args); } else result = "没有找到【assemblyName:" + assemblyName + "】.【className:" + className + "】.【methodName:" + methodName + "】方法"; } else result = "没有找到【assemblyName:" + assemblyName + "】.【className:" + className + "】类"; } catch (Exception myEx) { result = myEx.Message; } // context.Response.Write(CommonMethod.ToJson(result)); context.Response.Write(ToJson(result)); }
public bool IsReusable { get { return false; } }
#region public static string ToJson(object obj) /// <summary> /// 转Json /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToJson(object obj) { if (obj == null) obj = string.Empty; JavaScriptSerializer myJson = new JavaScriptSerializer(); if (obj.GetType().Name == "DataTable") { DataTable myDt = (DataTable)obj; string tableName = myDt.TableName == "" ? "TableName1" : myDt.TableName; Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add(tableName, RowsToDictionary(myDt)); return myJson.Serialize(dic); } else if (obj.GetType().Name == "DataSet") { DataSet myDs = (DataSet)obj; Dictionary<string, object> dic = new Dictionary<string, object>(); for (int i = 0; i < myDs.Tables.Count; i++) { string tableName = myDs.Tables[i].TableName == "" ? "TableName" + i.ToString() : myDs.Tables[i].TableName; dic.Add(tableName, RowsToDictionary(myDs.Tables[i])); } return myJson.Serialize(dic); } else return myJson.Serialize(obj); }
private static List<Dictionary<string, object>> RowsToDictionary(DataTable table) { List<Dictionary<string, object>> objs = new List<Dictionary<string, object>>(); foreach (DataRow dr in table.Rows) { Dictionary<string, object> drow = new Dictionary<string, object>(); for (int i = 0; i < table.Columns.Count; i++) { drow.Add(table.Columns[i].ColumnName, dr[i]); } objs.Add(drow); } return objs; }
#endregion } }
标签:http java os io for cti ar javascript
原文地址:http://www.cnblogs.com/huweijun/p/3881011.html