//欄目
//發送 ajax 請求
$.getJSON(
"../../../Templet/GetInfoHandler.ashx", //產生 JSON 數據的服務端頁面
{id: "0", sid: "1;2;3", rid: Math.round(Math.random() * 10) }, //向服務器發出的查詢字符串
//對返回的 JSON 數據進行處理
function(json) {
//循環取 json 中的數據,並呈現在列表中
$("#column_0_1").empty();
var colstr = '<h2><span><a href="#" href="#">更多>></a></span>熱門招聘</h2><ul>';
$.each(json, function(i) {
//alert(json[i].news_id);
colstr = colstr + '<li><a href="https://www.ishixi.com/news/html/' + json[i].new_file_name + '" href="https://www.ishixi.com/news/html/' + json[i].new_file_name + '">' + json[i].news_title + '</a></li>';
})
colstr = colstr + '</ul>';
$("#column_0_1").html(colstr);
alert("加載成功");
// })
用 Litjson 生成 json 數據的 C# 程序(服務端返回同結構的 JSON 數據):
//欄目
//發送 ajax 請求
$.getJSON(
"../../../Templet/GetInfoHandler.ashx", //產生 JSON 數據的服務端頁面
{id: "0", sid: "1;2;3", rid: Math.round(Math.random() * 10) }, //向服務器發出的查詢字符串
//對返回的 JSON 數據進行處理
function(json) {
//循環取 json 中的數據,並呈現在列表中
$("#column_0_1").empty();
var colstr = '<h2><span><a href="#" href="#">更多>></a></span>熱門招聘</h2><ul>';
$.each(json, function(i) {
//alert(json[i].news_id);
colstr = colstr + '<li><a href="https://www.ishixi.com/news/html/' + json[i].new_file_name + '" href="https://www.ishixi.com/news/html/' + json[i].new_file_name + '">' + json[i].news_title + '</a></li>';
})
colstr = colstr + '</ul>';
$("#column_0_1").html(colstr);
alert("加載成功");
// })
要點說明:$.getJSON() 是第一個參數為服務端地址、第二個參數為查詢字符串、第三個參數為回調函數的捷徑方法。回調函數中拿到的 json 已是解析好的對象,用 $.each() 遍歷後拼接 HTML 字符串,最後用 html() 一次性寫入目標容器,既高效又清晰。