jquery无法读取json对象并显示数据

时间:2013-03-07 11:29:36

标签: jquery asp.net

我正在尝试调用webmethod并获取json对象并使用jquery在aspx文件中显示数据。但是有些不对劲而且没有用。我将用下面的代码解释

这是webmethod

Database db = DatabaseFactory.CreateDatabase("Connection String2");
        DbCommand dbCommand;
        dbCommand = db.GetStoredProcCommand("MedBul_Select_Selected_Professional");
        db.AddInParameter(dbCommand, "id", DbType.Int16, Convert.ToInt16(id));
        IDataReader dr = db.ExecuteReader(dbCommand);
        if(dr.Read())
        {
            int p_id = Convert.ToInt16(dr["ProfessionalID"].ToString());
            string firstname = dr["ProfessionalName"].ToString();
            string lastname = dr["ProfessionalSurname"].ToString();
            int prefix = Convert.ToInt16(dr["PrefixID"].ToString());
            int gender = Convert.ToInt16(dr["Gender"].ToString());
            string birthdate = dr["BirthDate"].ToString();
            string mobilephone = dr["MobilePhone"].ToString();
            string email = dr["Email"].ToString();
            string diplomano = dr["DiplomaNo"].ToString();

            return_str += "[{\"id\":\"" + p_id + "\",\"firstname\":\"" + firstname + "\",\"lastname\":\"" + lastname + "\",\"prefix\":\"" + prefix + "\",\"gender\":\"" + gender + "\",\"birthdate\":\"" + birthdate + "\",\"mobilephone\":\"" + mobilephone + "\",\"email\":\"" + email + "\",\"diplomano\":\"" + diplomano + "\"}]";
        }

这是jquery代码。

  $('#btn_second').click(function () {
            //$('#txt_isim_4').val('test arif');
            $.ajax({
                type: "POST",
                url: "Registration.aspx/get_selected_professional",
                data: "{'id':'2'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    $.each(data, function (index, value) {
                        alert(value);
                        alert(value.d);
                        alert(index);
                        alert(value.firstname);
                    });

                }
            });
        });

我正在尝试提醒()返回的内容但它没有显示任何内容。我非常确定我可以从数据库中获取数据并正确解析....

我的代码出了什么问题?如何才能完成它以显示json对象?

2 个答案:

答案 0 :(得分:1)

看到你的回复..

return_str += "[{\"id\":\"" + p_id + "\",\"firstname\":\"" + firstname + "\",\"lastname\":\"" + lastname + "\",\"prefix\":\"" + prefix + "\",\"gender\":\"" + gender + "\",\"birthdate\":\"" + birthdate + "\",\"mobilephone\":\"" + mobilephone + "\",\"email\":\"" + email + "\",\"diplomano\":\"" + diplomano + "\"}]";

无需使用each循环..因为在ajax中您提到dataTypejson ..使用.运算符来获取对象< / p>

试试这个

success: function (data) {
              alert(data.id);
              alert(data.firstname); // similar for others
            }

答案 1 :(得分:0)

我假设返回的json看起来像

var $json ='[{"id":"1","name":"john"},{"id":"2","name":"smith"}]';
var json = $.parseJSON($json);//will already be parsed in your callback function bcoz of the dataType:json

尝试修改你的循环

$.each(json,function(i,j){
    $(j).each(function(k,v){
     console.log(v.id);
     console.log(v.name);
    });
});

http://jsfiddle.net/Eu9Pp/