您的位置:首页 > Web前端 > JQuery

jquery 获取json数据实现代码

2009-04-27 00:00 796 查看
//栏目 
//发送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="http://www.ishixi.com/news/html/' + json[i].new_file_name + '" href="http://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#程序:
//栏目 
//发送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="http://www.ishixi.com/news/html/' + json[i].new_file_name + '" href="http://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("加载成功"); 
// })

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); 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: