【问题标题】:Can't Load Data from Database using jquery ajax Asp.net webforms无法使用 jquery ajax Asp.net webforms 从数据库加载数据
【发布时间】:2016-04-15 05:14:31
【问题描述】:

我要从数据库中加载推文,但它不起作用请检查我在哪里做错了

这是我要加载推文的 div

<div id="load_tweets">
</div>

这是ready函数中的setinterval方法

setInterval(function () {
  $('#load_tweets').load("CS.aspx/fetch").fadeIn("slow");
  },1000);

这是 fetch [webMethod]

[System.Web.Services.WebMethod]
public static DataTable fetch()
{
SqlConnection con = new SqlConnection("data source=dbcomments;initial     catalog=CommentSystemUsingAjax;integrated security=true");
SqlDataAdapter da = new SqlDataAdapter("select * from tbl_tweet order by tweet_id desc", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}

我也试过这种方式

[System.Web.Services.WebMethod]
public static string fetch(string tweet)
{
SqlConnection con = new SqlConnection("data source=dbcomments;initial catalog=CommentSystemUsingAjax;integrated security=true");
SqlDataAdapter da = new SqlDataAdapter("select * from tbl_tweet order by tweet_id desc", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
    tweet = dt.Rows[0]["tweet"].ToString();
}
return tweet;

}

请帮帮我

【问题讨论】:

    标签: jquery asp.net ajax webforms


    【解决方案1】:

    你应该给出页面的确切链接到加载函数的参数

    setInterval(function () {
      $('#load_tweets').load("/CS.aspx/fetch").fadeIn("slow");
    },1000);
    

    还有你不能让我回来的数据表。你可以只返回字符串。您可以为此使用 json。你可以摆脱渲染 ASP Net Control

    【讨论】:

      【解决方案2】:

      将 Web Method 返回类型从数据表更改为字符串

      [WebMethod]
      public static string fetch()
      {
          SqlConnection con = new SqlConnection("connection string");
          SqlDataAdapter da = new SqlDataAdapter(query, con);
          DataTable dt = new DataTable();
          da.Fill(dt);
          return JsonConvert.SerializeObject(dt);     
      }
      

      现在使用 Ajax 调用静态方法...

      $(document).ready(function () {
          setInterval(function () 
          {
              FetchData();
          },1000);
      });
      
      function FetchData() {
          try {
              $.ajax({
                  async: true,
                  type: "POST",
                  contentType: "application/json; charset=utf-8",
                  url: "/Path/fetch",                    
                  dataType: "json",
                  success: function (data) {
                      if (data.d != "") {
                          var json_obj = $.parseJSON(data.d);
                          // get array of data in json_obj 
                      }
                  },
                  error: function (xhr, status, err) {
                  }
              });
          } catch (e) {
          }
      }
      

      【讨论】:

      • thnx 但是在 fetchData() 函数控制中没有进入 $.ajax({......我调试它控制的脚本直接从 $.ajax({ 到 catch 结束 curly括号.....现在我该怎么办?
      • 我应该在 document.ready 函数内部还是 document.ready 函数外部定义 FetchData 函数?
      • 将 FetchData 函数保留在 document.ready 之外并保持调试成功,即在您的 if 条件下
      • 当我删除 if 条件而不是它的工作,但它返回这样的数据 [{"tweet_id":48,"tweet":""},{"tweet_id":47,"tweet": ""},{"tweet_id":46,"tweet":""},{"tweet_id":45,"tweet":"sf"},{"tweet_id":44,"tweet":""}, {"tweet_id":43,"tweet":"sad"},
      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2013-01-03
      相关资源
      最近更新 更多