2012-08-12 50 views
0

我是一個新手開發者,並開始使用laravel來編碼。我的問題是,我無法通過菜單點擊來改變這個內容。我真的很困惑如何在這種情況下使用ajax。你能幫我用這個ajax代碼嗎?

我routes.php文件:

Route::get('articles',array('as'=>'articles','uses'=>'[email protected]')); 
Route::get('author/(:any)', array('as'=>'article','uses'=>'[email protected]')); 
Route::get('abouts',array('as'=>'abouts','uses'=>'[email protected]')); 

我default.blade.php(不久包含腳本)

<!DOCTYPE html> 
<head> 
    <title>{{$title}}</title> 
    {{ HTML::script('js/jquery.min.js') }} 
    {{ HTML::script('js/ajax.js') }} 
    ....... 
    </head> 
    <body> 
    ........ 
    <div id="wrapper"> 
    <div id="page"> 
       {{ $content }} 
    </div> 
    <!-- SIDEBAR --> 
    <div id="sidebar"> 
    <div id="sidebar-content"> 

    <ul id="menu"> 
    <li class="current"><a href="<?php echo URL::to_route('articles'); ?>">ANASAYFA</a></li> 
    <li><a href="<?php echo URL::to_route('abouts'); ?>">HAKKIMIZDA</a></li> 
....... 
    <script type="text/javascript">var BASE = "<?php echo URL::base(); ?>";</script> 
</body> 
</html> 

我的意見是內部DIV /#的內容。

我ajax.js:

$(document).ready(function() { 
    $('#sidebar-content ul li a').click(function(e) { 
     // prevent the links default action 
     // from firing 
     e.preventDefault(); 
     // attempt to GET the new content 
     $.get(BASE +'#content', function(data) { 
      $('#content').html(data); 
      console.log(data); 

     }); 
    }) 
}); 

我的文章控制器:

<?php 

class Articles_Controller extends Base_Controller { 

    public $restful = true; 
    public $layout = 'layouts.default'; 

    public function get_index(){ 
      $this->layout->title = "Anasayfa"; 
      $this->layout->content = View::make('articles.index') 
      ->with('articles',Article::order_by('id')->get()); 
     } 

     public function get_view($id){ 
      $this->layout->title = "Hizmetlerimiz"; 
      $this->layout->content = View::make('articles.view') 
      ->with('article',Article::find($id)); 
     } 

} 

回答

2

要替換的<div id="page">內容。

我想你應該寫$('#page').html(data);

+0

你好,我照你說的做。我得到所有的JavaScript重新加載。當我檢查console.log(data。)時,我可以看到,它獲取頁面的所有源代碼。我不能得到什麼錯誤? – ytsejam 2012-08-12 21:39:25

+0

您對'$ .get'的第一個參數是您從Ajax請求中獲得的URL。 'BASE +'#content''似乎是錯誤的(在Firebug中,你可以看到你準確得到哪個URL)。你想從href中獲得頁面。嘗試用'$(this).attr('href')'替換'BASE +'#content''。 – 2012-08-13 21:38:53