我正在尝试调用 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 对象?