0

我正在尝试调用 webmethod 并获取 json 对象并使用 jquery 在 aspx 文件中显示数据。但是出了点问题,它不起作用。我会用下面的代码解释

这是网络方法

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

                }
            });
        });

我正在尝试 alert() 返回的内容,但它不显示任何内容。我很确定我可以从数据库中获取数据并正确解析它......

我的代码有什么问题?如何完成以显示 json 对象?

4

2 回答 2

2

看到你的回复。。

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

不需要使用each循环..因为ajax你提到dataTypejson..使用.运算符来获取对象

试试这个

success: function (data) {
              alert(data.id);
              alert(data.firstname); // similar for others
            }
于 2013-03-07T11:36:46.533 回答
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/

于 2013-03-07T11:37:01.167 回答