第一部分:cs代码,文件类型ashx
//json输出 链接的分类
BLL_WebSiteGroupInfo bll_WebsiteGroupinfo = new BLL_WebSiteGroupInfo();
DataTable dt = bll_WebsiteGroupinfo.SelectWebGroupByUserId(userId);
StringBuilder builder = null;
builder = new StringBuilder();
builder.Append("[");
builder.Append("{groupId:1,groupName:'请选择'},");
builder.Append("{groupId:1,groupName:'未分类'},");
builder.Append("{groupId:2,groupName:'常用网址'},");
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
builder.Append("{");
builder.AppendFormat("groupId:" + dt.Rows[i]["GroupId"] + ",groupName:'" + dt.Rows[i]["Name"]+"'");
builder.Append("}");
builder.Append(",");
}
}
builder.Remove(builder.Length - 1, 1);
builder.Append("]");
string str = "{'group':" + builder + ",status:'true',type:'logined'}";
GoSuccess(str, 0);
public static void GoSuccess(string message, int gotype)
{
if (0 == gotype)
{
HttpContext.Current.Response.Write(message);
HttpContext.Current.Response.End();
}
else
{
HttpContext.Current.Response.Write("<script>alert('" + message + "');history.back(-1)</script>");
HttpContext.Current.Response.End();
}
}
第二部分:js代码
$(document).ready(function(){
$.ajax({
type: "POST",
url: "../MyLinks/HandlerForCollecting.ashx?title="+escape(title)+"&url="+url+"&name="+name+"&pwd="+pwd,
data: "value=ajax",
success: function(msg){
var url= eval("("+msg+")");// *把JSON格式的字符串转换成js对象
setCookie("GroupList",msg);
var key, counter = 0;
for(key in url.group) counter++;
//解析json,添加 分类(json:登录状态,消息,分类集合)
for(var i=0;i<counter;i++)
{
var valueItem=new Option(url.group[i].groupName,url.group[i].groupId);
document.getElementById("ddlGroup").options.add(valueItem);
}
if(url.status=="true")//msg=="登录成功!"
{
alert("登录成功!");
setCookie("Username",document.getElementById('txtUsername').value);
setCookie("Password",document.getElementById('txtPassword').value);
document.getElementById("txtTitle").value=unescape(request('title'));
document.getElementById("txtUrl").value=request('titleurl');
document.getElementById("login").style.display="none";
document.getElementById("collect").style.display="block";
}
if(url.status=="faild")
{
alert("用户名或密码错误!");
return;
}
}
});
});
}