public string AjaxJson() { string method = Request.QueryString["method"]; //是否将结果包含在结构体内,默认为包含,withstruct=y withstruct=n string withstruct = Request.QueryString["withstruct"];//y/n string returntype = Request.QueryString["returntype"]; if (withstruct == null || withstruct == "") { withstruct = "y"; //return Json("", JsonRequestBehavior.AllowGet); } var result = ""; if (method == null || method == "") { JDataResult jdataResult = new JDataResult(); jdataResult.resultcode = 200; jdataResult.apicode = 2; jdataResult.reason = "参数错误,请提供参数[method]"; return("{'err':'参数错误,请提供参数[method]'}"); } if (returntype == null) { returntype = "json"; } //获取所有参数; Hashtable parsHashtable = new Hashtable(); for (int i = 0; i < Request.Params.Keys.Count; i++) { string key = Request.Params.GetKey(i); string value = Request.Params[key]; parsHashtable.Add(key, value); } ApiResult apiresult = ApiExec.Done(parsHashtable); switch (apiresult.ResultDataType) { case 0: //txt result = apiresult.Data.ToString(); break; case 1: //json result = apiresult.Data.ToString(); break; default: result = "{'err':'无效的数据返回类型,请使用json类型'}"; break; } return(result); }
public ActionResult XmlData(Hashtable hashtable) { try { ApiResult apiresult = ApiExec.Done(hashtable); if (apiresult.Code > 0) { return(Json(apiresult.msg, JsonRequestBehavior.AllowGet)); } else { if (apiresult.ResultDataType == 4) { DataTable dt = apiresult.DataTable; // 序列化为XML格式显示 XmlResult xResult = new XmlResult(dt, dt.GetType()); return(xResult); } else if (apiresult.ResultDataType == 2) { //DataTable dt = apiresult.DataTable; // 序列化为XML格式显示 //XmlResult xResult = new XmlResult(dt, dt.GetType()); return(new XmlResult(apiresult.Data.ToString(), typeof(string))); } else { //return Json(SerializeDataTableXml(dt), JsonRequestBehavior.AllowGet); return(new XmlResult("无法转换成xml输出", typeof(string))); } } } catch (Exception exp) { return(new XmlResult(exp.Message, typeof(string))); //return Json(exp.Message, JsonRequestBehavior.AllowGet); } }
public JsonResult JData(Hashtable hashtable, string withstruct) { Response.ContentType = "applicaton/json"; try { ApiResult apiresult = ApiExec.Done(hashtable); if (apiresult.Code > 0) { JDataResult jdataResult = new JDataResult(); jdataResult.resultcode = 200; jdataResult.apicode = apiresult.Code; jdataResult.reason = apiresult.msg; return(Json(jdataResult, JsonRequestBehavior.AllowGet)); } else { JDataResult jdataResult = ConvertToJDataResult(apiresult); if (withstruct.ToLower() == "y") { return(Json(jdataResult, JsonRequestBehavior.AllowGet)); } else { return(Json(jdataResult.result, JsonRequestBehavior.AllowGet)); } } } catch (Exception exp) { JDataResult jdataResult = new JDataResult(); jdataResult.resultcode = 200; jdataResult.apicode = 2; jdataResult.reason = "API数据处理子件通讯异常!" + exp.Message; return(Json(jdataResult, JsonRequestBehavior.AllowGet)); //return Json(exp.Message, JsonRequestBehavior.AllowGet); } }
public ActionResult AjaxJsonp() { string method = Request.QueryString["method"]; //是否将结果包含在结构体内,默认为包含,withstruct=y withstruct=n string withstruct = Request.QueryString["withstruct"];//y/n string returntype = Request.QueryString["returntype"]; string callback = (string)Request.QueryString["callback"]; string result = ""; if (withstruct == null || withstruct == "") { withstruct = "y"; //return Json("", JsonRequestBehavior.AllowGet); } JDataResult jdataResult = new JDataResult(); if (method == null || method == "") { jdataResult.resultcode = 200; jdataResult.apicode = 2; jdataResult.reason = "参数错误,请提供参数[method]"; //result = "{'err':'参数错误,请提供参数[method]'}"; return(new JsonpResult <object>(jdataResult, callback)); } if (returntype == null) { returntype = "json"; } //获取所有参数; Hashtable parsHashtable = new Hashtable(); for (int i = 0; i < Request.Params.Keys.Count; i++) { string key = Request.Params.GetKey(i); string value = Request.Params[key]; parsHashtable.Add(key, value); } ApiResult apiresult = ApiExec.Done(parsHashtable); // if (apiresult.Code > 0) { //程序异常; return(new JsonpResult <object>(apiresult, callback)); } jdataResult = ConvertToJDataResult(apiresult); if (withstruct.ToLower() == "y") { return(new JsonpResult <object>(jdataResult, callback)); //return Json(jdataResult, JsonRequestBehavior.AllowGet); } else { //return Json(jdataResult.result, JsonRequestBehavior.AllowGet); return(new JsonpResult <object>(jdataResult.result, callback)); } //return new JsonpResult<object>(new { result = result }, callback); }