首先要明白這個是前台去請求后台的接口,把前台的參數傳給后台,然后后台給前台一個或多個返回結果。
1.后台的代碼如下:
/// <summary> /// GetInfomationDetail 的摘要說明 /// </summary> public class GetInfomationDetail : IHttpHandler { public void ProcessRequest(HttpContext context) { DB_Infomation db_info = new DB_Infomation(); Dictionary<String, Object> dicParameter = GetParameter(context); Hashtable ht = new Hashtable(); string infoid = dicParameter["infoid"].ToString(); context.Response.ContentType = "text/plain"; context.Request.ContentEncoding = Encoding.GetEncoding("UTF-8"); context.Response.ContentEncoding = Encoding.GetEncoding("UTF-8"); context.Response.Charset = "UTF-8"; int allcount =db_info.PublishListCount(infoid); ht.Add("allcount", allcount); DataView dView = db_info.getInfoDetail(infoid); if (dView.Count > 0) { string title = dView[0]["title"].ToString(); string secondtitle = dView[0]["secondtitle"].ToString(); string infolock = dView[0]["InfoLock"].ToString(); //信息是否鎖住 ht.Add("title", title); ht.Add("secondtitle", secondtitle); ht.Add("infolock", infolock); } context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); context.Response.Write(CreateJsonParams(ht)); } public bool IsReusable { get { return false; } } private Dictionary<String, Object> GetParameter(HttpContext context) { StreamReader reader = new StreamReader(context.Request.InputStream); //得到json字符串:strJson={"key3":"xdp-gacl","key4":"白虎神皇"} String strJson = HttpUtility.UrlDecode(reader.ReadToEnd()); JavaScriptSerializer jss = new JavaScriptSerializer(); //將json字符串反序列化成一個Dictionary對象 Dictionary<String, Object> dicParameter = jss.Deserialize<Dictionary<String, Object>>(strJson); return dicParameter; } public string CreateJsonParams(Hashtable items) { string returnStr = ""; foreach (DictionaryEntry item in items) { returnStr += "" + item.Key.ToString() + ":\"" + item.Value.ToString() + "\","; } return "{" + returnStr.Substring(0, returnStr.Length - 1) + "}"; } }
2.前台代碼如下:注意,這個是一個js方法,可以在初始化的時候調用此方法。其中的async,為false,可以保證當這個頁面在初始化調用多個接口的時候,鎖住當前的接口,避免接口錯亂,導致接口返回error,但是這個接口已經執行完成。
function BindInfo(){ var infoid = document.getElementById("infoid").innerHTML; var requestData = "{'infoid': \"" + infoid + "\"}"; var prexUrl = "/hkjy/"; $.ajax({ type: "POST", cache: false, async: false, url: prexUrl + "SmtpService/HkmhWebService/GetInfomationDetail.ashx", contentType: "application/x-www-form-urlencoded; charset=UTF-8", data:requestData, success: function (data) { var data = eval("(" + data + ")"); var title = data.title; var secondtitle = data.secondtitle; var infolock = data.infolock; var allcount = data.allcount; if(infolock=="0"){ infolock = "【進行中】"; } else{ infolock="【已結束】"; } document.getElementById("infoTitle").innerHTML=title; document.getElementById("infoSecondTitle").innerHTML=secondtitle; document.getElementById("infoStatus").innerHTML=infolock; document.getElementById("spanCount").innerHTML=allcount; }, error: function (XMLHttpRequest, textStatus, errorThrown) {//請求錯誤 時執行的方法 // alert("error!" + errorThrown); alert("網絡異常"); } });