dongqing220586 2012-08-08 19:47
浏览 28
已采纳

AJAX不在这里工作和一些问题?

After much research, I have found that Laravel is very nice. I have a homepage opening with an animation and after it the menu and page content splits into each side. With the links clicked, I only want content to change. I tried jQuery/ajax to work with. But I have a question, I want the links inside the content and sidebar-menu to change content. Can you show me the way to do that?

This is my ajax.js :

 $(document).ready( function() {

    $("#content ul li a").click( function(e){
         e.preventDefault();
         $('#page').load(this.href);
         return false;
    });

});

This is my Articles controller:

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

My default.blade.php:

<div id="wrapper">
    <div id="page">
        {{ $content }}
    </div>

    <div id="sidebar">
        <div id="sidebar-content">
        <!-- MENU -->
        <ul id="menu">
            <li class="current"><a href="#">ANASAYFA</a></li>
            <li><a href="#">HAKKIMIZDA</a></li>
// ...

My index.blade.php:

<div id="content">

    @foreach($articles as $article)
    <ul class="blog-medium">
        <li>    
            <div class="blog-medium-text">
                <h1><a href="./post.html">{{$article-> title }} </a></h1>
                <p class="blog-medium-excerpt"><p>Tek kişilik sınıflarda YGS - LYS hazırlık programımızda dersler özel&#8230;<br />
                {{ HTML::link_to_route('article','devamı', array($article->id)) }}</p>
            </div>
        </li>
    </ul>
    @endforeach
</div>

My view.blade.php:

<div id="content">
    <h1>{{$article->title}}</h1>
    <p>{{$article->content}}</p>
</div>
  • 写回答

1条回答 默认 最新

  • douhuang2218 2012-08-09 18:53
    关注

    I think the problem is in your Jquery

    $(function() 
    {
        $("#content ul li a").click( function()
        {
            var url = $(this).attr('href'); // Get href value from <a>
    
            $('#page').load(url);
    
            return false; // No need for e.preventDefault() if return false;
        });
    });
    

    This should work assuming that the href value is the full or relative path to the controllers method.

    As you are using restful controllers, consider that AJAX is capable of GET and POST HTTP methods. You can segregate all POST and GET data into seperate functions and also check if the request is Ajax in Laravel using:

    if (Request::ajax())
    {
        ... // The request is AJAX, do ajax stuff
    
        return Response::json(array('action' => 'complete'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100