js eval()执行传参函数的写法
.cs
public class Message<T> { // 数据总数 public int? Total { get; set; } // 关键数据 public List<T> Rows { get; set; } // 信息状态 // 0:失败,1:成功, 2:其它 public int? Status { get; set; } // 返回信息 public string Msg { get; set; } //js前台执行方法 public string JsExecuteMethod { get; set; } public override string ToString() { return "Total:" + this.Total + ",Rows:" + this.Rows + ",Status:" + this.Status + ",Msg:" + this.Msg; } }
.ashx.cs
public string QueryWarnInfoByZhuanId(HttpContext context) { string ret = null; var message = new Message<WarnRec>(); var warnrecbll = new WarnRecBll(); var id = context.Request.Params["zhuanid"]; if (string.IsNullOrEmpty(id)) { return ret; } var zhuanid = int.Parse(id); try { var dt = warnrecbll.FindBy(zhuanid); var list = ConvertHelper<WarnRec>.ConvertToList(dt); message.Rows = list; message.Status = 1; message.Msg = "返回成功"; message.JsExecuteMethod = "respose"; ret = _jss.Serialize(message); } catch (Exception) { message.Status = 0; message.Msg = "返回失败"; Log.Error(message.ToString()); } return ret; }
.js
/** * *ajax成功时返回resultObject是json数据 **/ function serviceSuccess(resultObject) { if (resultObject==null) { return true; } switch (resultObject.Status) { case 0: case 2: parent.messager("提示", resultObject.Msg); break; case 1: eval(resultObject.JsExecuteMethod +"(resultObject)"); break; default: } }
/** * ajax成功返回时 * 显示告警信息data到页面 **/ function respose(data) { var resHtml = ""; for (var i = data.Rows.length - 1; i >= 0; i--) { resHtml += "<li>" + data.Rows[i].LogType + " " + eval("new " + data.Rows[i].Occurdt.split('/')[1]).Format("yyyy-MM-dd HH:mm:ss") + " " + data.Rows[i].LogDesc + " " + "</li>"; } $("#response").html(resHtml); }