【问题标题】:Webmethod jquery ajax call not returning any resultsWebmethod jquery ajax调用不返回任何结果
【发布时间】:2018-01-09 05:10:36
【问题描述】:

您好,我正在构建一个网络媒体库,

我有一个从数据库中获取标签的简单网络方法。

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;
                }
            }
        }

和一个我在按钮单击时调用的 javascript 以在 div 中显示结果。 但我在通话中没有得到任何结果。也没有显示任何错误。

 $(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>. ");
                })
            };
        }

【问题讨论】:

  • 大家好,我已经分别测试了给出的所有三个建议。尚无阳性结果。

标签: javascript jquery asp.net ajax webmethod


【解决方案1】:

您在 cmd.CommandText = "GetTagCount"; 之后缺少一行它是 cmd.CommandType = CommandType.StoredProcedure;

【讨论】:

  • 没有帮助。
  • 你是对的。但这不是根本原因。我的服务根本没有被调用。感谢您的帮助。
【解决方案2】:

在 Web.Config 中设置:

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

您的网络服务代码将是:

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();                    
        }
    }
}

【讨论】:

  • 没有结果。没有变化。
【解决方案3】:

我想我找到了解决方案。

当我尝试使用 chrome 的网络选项卡进行故障排除时..我得到了这个结果。

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

据我了解,这是因为模板中的配置

在 App_Start/RouteConfig.cs 这一行:

settings.AutoRedirectMode = RedirectMode.Permanent;

在其中一篇关于 SO (Authentication failed during call webmethod from jquery.ajx with AspNet.FriendlyUrls and AspNet.Identity) 的帖子中

建议发表评论。

现在已经完成了。任何人都可以建议评论这条线的利弊。如果从安全的角度来看这不是一个好主意,有什么解决方法吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多