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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?