2013-03-07 92 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/