2016-04-15 25 views
0

我从数据库要去负载鸣叫加载数据,但它不能正常工作,请检查在那里我做错误无法从数据库使用jQuery的ajax Asp.net web表单

这是div在哪里要加载的鸣叫

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

这是准备函数内部的setInterval方法

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

,这是取[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; 

} 

请帮我

回答

0
从数据表

更改Web方法返回类型为字符串

[WebMethod]

公共静态字符串取()

{

的SqlConnection CON =新的SqlConnection( “连接字符串”);

SqlDataAdapter da = new SqlDataAdapter(query,con);

DataTable dt = new DataTable();

da.Fill(dt);

reurn JsonConvert.SerializeObject(dt);

}

现在打电话使用Ajax静态方法...

$(文件)。就绪(函数(){

的setInterval(函数(){

FetchData( );

},1000); });

功能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) { 

     } 
    } 
+0

thnx但在fetchData()函数控制不进入$ .ajax({...我调试它直接从$控制跳过的脚本。ajax({到catch关闭花括号.....现在我该怎么办? – Heartlion

+0

和我应该在哪里定义FetchData函数在document.ready函数内部还是外部document.ready函数? – Heartlion

+0

保留FetchData函数如果条件 –

0

你应该给页面的确切链路负载功能

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

也回来了数据表,你不能让我的参数。你可以返回字符串。你可以使用json来做到这一点。你可以逃脱渲染ASP网络控制

+0

不工作...... :( – Heartlion

相关问题