2013-03-07 58 views
0

我想调用一个webmethod并获取json对象并在jquery中显示aspx文件中的数据。但有些事情是错误的,它不起作用。我将下面jquery无法读取json对象并显示数据

代码解释这里是将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对象?

+0

你检查数据,看数据是否存在? – karthick 2013-03-07 11:31:25

+0

显示你的json结果。 – 2013-03-07 11:31:26

+0

也发布您的数据对象... – bipen 2013-03-07 11:31:53

回答

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 ..使用.接线员给的对象

试试这个

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

作为它的对象数组不应该是像'data [0] .id' – dakait 2013-03-07 11:37:53

+0

否...''dataType:'json''的ajax会自动为你解析..试试吧.. :) – bipen 2013-03-07 11:38:50

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/