.net前端部分
<body>
<div class="table-content">
</div>
<button class="get-users" id="get-users">获取用户列表 </button>
<script src="http://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script>
$('.get-users').on('click', function () {
$.get('default.aspx?action=getusers', function (data) {
var json_data = JSON.parse(data);
var userlist = json_data.user;
var table_html_str = BuildTable(userlist);
$('.table-content').html(table_html_str);
})
})
function BuildTable(user) {
var html_str = '<table><th>用户名</th><th>手机号</th>';
for (var i = 0; i < user.length; i++) {
html_str += '<tr>'
+ '<td>' + user[i].username + '</td>'
+ '<td>' + user[i].tel + '</td>'
+ '</tr>'
}
html_str += '</table>';
return html_str;
}
</script>
</body>
.net后端部分
public partial class _default : System.Web.UI.Page
{
public class User
{
public User(string name, string phone)
{
username = name;
tel = phone;
}
public string username { get; set; }
public string tel { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.QueryString.Get("action");
if (action == "getusers")
{
string input = "username1, pwd1, 1311111111; username2, pwd2, 1311111111; username3, pwd3, 1311111111";
string[] user_infos = input.Trim().Split(';');
List<User> user_list = new List<User>();
foreach (string userinfo in user_infos)
{
string[] info_metas = userinfo.Split(',');
string username = info_metas[0];
string tel = info_metas[2];
User user = new User(username, tel);
user_list.Add(user);
}
if (user_list.Count > 0)
{
var rsp_obj = new
{
code = 200,
user = user_list
};
Response.Write(JsonConvert.SerializeObject(rsp_obj));//将rsp_obj转化为json并输出
Response.End();
}
}
}
网友评论