首先要明白这个是前台去请求后台的接口,把前台的参数传给后台,然后后台给前台一个或多个返回结果。
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("网络异常"); } });
