jquery 獲取json數據實現代碼
更新時間:2009年04月27日 17:53:55 作者:
jquery json數據獲取代碼。
復制代碼 代碼如下:
//欄目
//發(fā)送ajax請求
$.getJSON(
"../../../Templet/GetInfoHandler.ashx", //產生JSON數據的服務端頁面
{id: "0", sid: "1;2;3", rid: Math.round(Math.random() * 10) }, //向服務器發(fā)出的查詢字符串
//對返回的JSON數據進行處理
function(json) {
//循環(huán)取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 + json[i].new_file_name + '" + json[i].new_file_name + '">' + json[i].news_title + '</a></li>';
})
colstr = colstr + '</ul>';
$("#column_0_1").html(colstr);
alert("加載成功");
// })
用Litjson生成json數據的C#程序:
復制代碼 代碼如下:
//欄目
//發(fā)送ajax請求
$.getJSON(
"../../../Templet/GetInfoHandler.ashx", //產生JSON數據的服務端頁面
{id: "0", sid: "1;2;3", rid: Math.round(Math.random() * 10) }, //向服務器發(fā)出的查詢字符串
//對返回的JSON數據進行處理
function(json) {
//循環(huán)取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 + json[i].new_file_name + '" + json[i].new_file_name + '">' + json[i].news_title + '</a></li>';
})
colstr = colstr + '</ul>';
$("#column_0_1").html(colstr);
alert("加載成功");
// })
jquery通過json獲取數據
復制代碼 代碼如下:
<script type="text/javascript">
$(document).ready(function () {
getScatalog("paidang", "M06");
});
function getScatalog(selectid,BaseCode) {
if (BaseCode != "") {
$.ajax({
url: "ajax/getCatalogByBasecode.aspx",
data: "code=" + encodeURI(BaseCode), cache: false,
datatype: "html",
success: function (context) {
fillselect(selectid, context);
}
});
}
else {
return "Error";
}
}
function fillselect(selectid, context) {
var listitem=new Array();
listitem = eval(context);
for (var i = 0; i < listitem.length; i++) {
$("#" + selectid).append("<option value='" + listitem[i]["code"] + "'>" + listitem[i]["name"] + "</option>"); //為Select追加一個Option(下拉項)
}
}
</script>
html代碼:
復制代碼 代碼如下:
<select id="paidang" class="selectstyle" name="paidang">
<option value="" selected>==請選擇==</option>
</select>
Ajax:
新建一個.aspx頁面刪除.aspx頁面里的html代碼刪除,在.aspx.cs里添加如下代碼
復制代碼 代碼如下:
string rq_basecode=null;
rq_basecode = Request.QueryString["code"];
if (string.IsNullOrWhiteSpace(rq_basecode))
{
Response.Write("Error");
Response.End();
}
BLLCataLog bll_info = new BLLCataLog();
List<Scatalog> lt_info = new List<Scatalog>();
lt_info = bll_info.GetCatalog(rq_basecode,"");
//Response.Write(rq_basecode);
if (lt_info.Count > 0)
{
Response.Write(JsonHelper.ToJson(lt_info));
}
else
{
Response.Write("Null");
}
BLL層的數據:
復制代碼 代碼如下:
public List<M2Model.Scatalog> GetCatalog(string code, string refcode)
{
DALCataLog dalcatalog6 = new M2SharpDAL.DALCataLog();
return dalcatalog6.GetCatalog(code, refcode);
}

