发布于 2017-07-14 04:38:41 | 113 次阅读 | 评论: 0 | 来源: 网友投递
HTML 超文本标记语言
超文本标记语言,标准通用标记语言下的一个应用。
static Handler()
{
hs = new Dictionary<string, Func<string>>();
hs.Add("add", delegate()
{
int id = int.Parse(req("id"));
string title = req("title");
return "add";
});
hs.Add("update", delegate()
{
int id = int.Parse(req("id"));
string title = req("title");
return "update";
});
}
context.Response.ContentType = "text/plain";
HttpRequest req = context.Request;
string action = req["action"].ToLower();
string result = hs[action]();
context.Response.Write(result);
static string req(string key)
{
return HttpContext.Current.Request[key];
}
static string jss(object obj)
{
JavaScriptSerializer JSS = new JavaScriptSerializer();
return JSS.Serialize(obj);
}
function success() {
if ( s.global ) {
trigger( "ajaxSuccess", [xhr, s] );
}
// If a local callback was specified, fire it and pass it the data
if ( s.success && xhr.responseText!="unlogin" ) {
s.success.call( callbackContext, data, status, xhr );
}
}
$(document).ajaxSuccess(function(event,xhr,settings){
if(xhr.responseText=="unlogin"){
window.top.location.href="/login.html";
}
})
if (HttpContext.Current.Request.UrlReferrer.ToString().ToLower().IndexOf("login.html") < 0)
{
if (HttpContext.Current.Session["user"] == null)
{
HttpContext.Current.Response.Write("unlogin");
HttpContext.Current.Response.End();
}
}
<ul id="ulList">
<li><a href="somepage.html?id={ID}">{Title}</a><br />
{Content}</li>
</ul>
$(function() {
var ulList = $("#ulList");
ulList.data("tpl",ulList.html()).empty();
}
public class News
{
public int ID { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
//handler里用了上面第一个问题的解决方法
hs.Add("getNews", delegate()
{
List<News> list = new List<News>()
{
new News(){ ID=1,Title="title1",Content="Content1"},
new News(){ ID=2,Title="title2",Content="Content2"},
new News(){ ID=3,Title="title3",Content="Content3"},
};
return jss(list);
});
$.get("Handler.ashx?n=" + Math.random(), { action: "getNews" }, function(data) {
var list = $.parseJSON(data);
var ul = $("#ulList");
var html = "";
for (var i = 0; i < list.length; i++) {
html += ul.data("tpl").format(list[i]);
}
ul.html(html);
})