2012-03-22 67 views
0

我有这段代码,我该如何让它成为ajax jquery?我有一个正在从数据库拉的文章的页面,我希望一旦用户点击按钮刷新Ajax调用将刷新文章。ajax jquery刷新数据库中的数据

  <!-- refresh the page with jquery !--> 
      <script type="text/javascript"> 
       $(document).ready(function() {  
       $('#Button_refresh').click(function() {  
       $.ajax({ 
}); }); 
      </script> 

回答

0

客户端javascript无法从数据库中进行查询。那么,至少它不是如何在安全的应用程序中完成的。你可以从后端(php/asp/python/nodejs ..)询问应该这样做的情况。

如果你的意思是,以唤起一些DIV通过URL从后端的文章,那么就

$.get(URL, function(response){ 
    $('#wrapper').html(response); 
}); 
0

您可以通过一个文件夹中创建一个PHP文件(如果你使用PHP)和使用做到这一点将php文件作为控件(用于检索数据)。首先你必须做ajax“Post”呼叫。下面是一些代码:

$.ajax({ 
     type: "POST", 
     url: "linktoyourphpfile.php", 
      data: { refresh: articles }, 
      success: function(data) 
        { 
       $("#div-id-where-articles-are-generated").html(data); 

        } 
      }); 

      }); 

现在正在通话后,您可以编辑PHP文件

if (isset($_POST['refresh']) { 

// you do the query here. It will be an array. So you need a to define a string and accumulate whatever you need to be written. here's a sample. 
$str=""; //first define the string. 

$resultset //is the array of the results returned by the database. then loop through it. 

foreach($resultset as $res) 
    { 
     $str=$str."<div>$res['somethin']</div>"; //you got the point. 

    } 
//then at the end just echo the str. 

echo $str; 
}