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);
}