-1

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>. ");
                })
            };
        }
Mashhoor Gulati
  • 127
  • 3
  • 13

3 Answers3

1

You are missing a line after cmd.CommandText = "GetTagCount"; it is cmd.CommandType = CommandType.StoredProcedure;

Rahul
  • 130
  • 2
0

Setting this in Web.Config:

<system.web.extensions>
    <scripting>
      <webServices>
          <jsonSerialization maxJsonLength="2147483647"/>
      </webServices>
    </scripting>
</system.web.extensions>

You webservice code will be:

public class Tags
{
    public string tag_ID { get; set; }
    public string tag { get; set; }
    public string total_count { get; set; }
}

[WebMethod]
public static void 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();

            System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            string result = jSearializer.Serialize(tag);
            System.Web.HttpContext.Current.Response.ContentType = "application/json";
            System.Web.HttpContext.Current.Response.Write(result);
            System.Web.HttpContext.Current.Response.End();                    
        }
    }
}
Ravikumar
  • 613
  • 1
  • 9
  • 22
0

I guess I found the solution.

When I tried to troubleshoot using chrome's network tab..I got this result.

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

From which I understood that its because of configuration in template

in App_Start/RouteConfig.cs this line:

settings.AutoRedirectMode = RedirectMode.Permanent;

In one of the post on SO (Authentication failed during call webmethod from jquery.ajx with AspNet.FriendlyUrls and AspNet.Identity)

it was suggested to comment it.

Now since this is done. Can any one suggest pros & cons of commenting out this line. If its not a good idea from security point of view, any workaround.?

Mashhoor Gulati
  • 127
  • 3
  • 13