首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

一个关于json和ajax的有关问题

2013-12-02 
一个关于json和ajax的问题前台:function Change_Select(){var id $(#sheng).val()$.ajax({async:fals

一个关于json和ajax的问题
前台:  
 function Change_Select()
        {
            var id = $("#sheng").val();
            $.ajax({
                async:false,
                type: 'POST',           
                url: "tools/changesheng.ashx",
                dataType: 'json',
                data: { sid: id },
                success: function (data) {
                    alert(data);
                }
            });
        }
.cs:

           context.Response.ContentType = "text/plain";
            int id = Convert.ToInt32(context.Request.Form["sid"]);
            DataSet dt= cityd.GetList("RootID="+id);          
            context.Response.Write(GetJsonByDataset(dt));
            context.Response.End();


返回的json是一个object,内容是根据前一个下拉框的选择而出现的,如


{"ok":true,"ds":
[{"CityID":"101","CityName":"石家庄市","RootID":"5","Child":"0","Layer":"2","Sort":"1","Isopen":"1"},
{"CityID":"102","CityName":"唐山市","RootID":"5","Child":"0","Layer":"2","Sort":"2","Isopen":"1"},
{"CityID":"103","CityName":"秦皇岛市","RootID":"5","Child":"0","Layer":"2","Sort":"3","Isopen":"1"},
{"CityID":"104","CityName":"邯郸市","RootID":"5","Child":"0","Layer":"2","Sort":"4","Isopen":"1"},
{"CityID":"105","CityName":"邢台市","RootID":"5","Child":"0","Layer":"2","Sort":"5","Isopen":"1"},
{"CityID":"106","CityName":"保定市","RootID":"5","Child":"0","Layer":"2","Sort":"6","Isopen":"1"},
{"CityID":"107","CityName":"张家口市","RootID":"5","Child":"0","Layer":"2","Sort":"7","Isopen":"1"},
{"CityID":"108","CityName":"承德市","RootID":"5","Child":"0","Layer":"2","Sort":"8","Isopen":"1"},
{"CityID":"109","CityName":"沧州市","RootID":"5","Child":"0","Layer":"2","Sort":"9","Isopen":"1"},
{"CityID":"110","CityName":"廊坊市","RootID":"5","Child":"0","Layer":"2","Sort":"10","Isopen":"1"},
{"CityID":"111","CityName":"衡水市","RootID":"5","Child":"0","Layer":"2","Sort":"11","Isopen":"1"}]
}


现在我想把CityName加在第二个下拉框里面,请教这个json要怎么使用呢?
[解决办法]

引用:
Quote: 引用:


    $(data.ds).each(function (i,n) {
        $("#select2")[0].options.add(new Option(n.CityName, n.CityID));
    });
为什么我alert(data.ds)是undefind啊

那你要看看你的返回值了.
[解决办法]

success: function (data) {
                   var jsonstr = JSON.stringify(StuArrayObjs); 
                   var json = eval(TeachCourseJsonString); 
                   //json这个就是Json对象
                      json[0].CityName  //这样访问  也可以循环json对象
                }

热点排行