Hi I am building a web media gallery for which,
I've a simple webmethod to fetch tags from database.
public class Tags
{
public string tag_ID { get; set; }
public string tag { get; set; }
public string total_count { get; set; }
}
[WebMethod]
public static List<Tags> GetTags()
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["taggerConnectionString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "GetTagCount";
cmd.Connection = conn;
List<Tags> tag = new List<Tags>();
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
tag.Add(new Tags
{
tag_ID = sdr["tag_ID"].ToString(),
tag = sdr["tag"].ToString(),
total_count = sdr["total_count"].ToString()
});
}
}
conn.Close();
return tag;
}
}
}
and a javascript which I call on button click to display the result in a div. But I am not getting any result in the call. No errors are also being displayed.
$(document).ready(function () { $('#getTags').click(myFunction); });
function myFunction() {
$.ajax({
type: "POST",
url: "/App/WebForm1.aspx/GetTags",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
function OnSuccess(response) {
var Tags = response.d;
$(Tags).each(function () {
var tag = this.tag;
$("#results").append(" <b>" + tag + "</b>. ");
})
};
}