【问题标题】:Laravel 5.1 - Ajax Navigation not working with Javascript history APILaravel 5.1 - Ajax 导航不适用于 Javascript 历史 API
【发布时间】:2016-01-26 15:35:01
【问题描述】:

我目前正在开发一个需要通过 ajax 从导航菜单加载我的视图的应用。

我的菜单:

<div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <a class="logo menu-link" data-route="index" href="{{ url('/') }}"><img src="{{ asset('img/logo.png') }}" alt="my-app-logo"></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav navbar-right">
        <li><a class="menu-link" href="" data-route="studio">Studio</a></li>
        <li><a class="menu-link" href="" data-route="customers">Customers</a></li>
        <li><a class="menu-link" href="" data-route="bio">Bio</a></li>
        <li><a class="menu-link" href="" data-route="contacts">Contacts</a></li>
    </ul>
</div>

我的路线:

Route::get('/', 'PagesController@index');
Route::get('/{page}', 'PagesController@loadPage');

我的控制器:

class PagesController extends Controller {

    // Index
    public function index(){
        return view('pages.index');
    }

    // load page
    public function loadPage($page){
        return view('ajax.'.$page)->render();
    }

}

一个视图示例(这里我传递纯 html 而不扩展刀片模板):

<div class="content">
    <div class="studio">
        <div class="custom-area">
            <div class="page-title-box">
                <h3 class="page-number">N. 67</h3>
                <h1 class="page-title">Area Test</h1>
            </div>
        </div>
    </div>
</div>

我的 Javascript:

// in my document.ready    

// get url page name and load my view at refresh page
var urlFull = window.location.href,
    pageName = urlFull.split('/').pop();

// currently only my index has blade functions, other pages content is pure html
if(pageName != ''){
    loadContentPage(pageName);
}

// get view from browser's navigation
window.onpopstate = function(event) {
    loadContentPage(pageName);
}

// Laravel setup for ajax calls
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content-wrapper')
    }
});

var route;
$('.menu-link').on('click', function(){

    // active link
    $('.menu-link').removeClass('active');
    $(this).addClass('active');

    // load content
    route = $(this).data('route');

    // If i pass an empty string to simply call my index, in console i have an error(*)
    if(route == 'index'){ route = ''; }
    loadContentPage(route);

    // history
    history.pushState(route, null, route);

    return false;
});

// end document.ready

function loadContentPage(page){
    $('#content-wrapper').children().fadeOut(150, function(){
        $('#content-wrapper').load("{{ url('/') }}/" + page, function(data){
            $(this).html(data);
        });
    });
}

(*) 第一个问题是在控制台中我得到了这个:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

如果我刷新页面时我有例如http://localhost:8888/app-test/public/studio 我只得到 html,我想知道:

  1. 如何解决这个问题
  2. 如果我在我的路由中传递第二个参数 需要这样的页面详细信息:

    (Route::get('/{page}/{detail}', 'PagesController@loadPage'));

编辑:在函数中添加变量,历史现在可以工作了:

window.onpopstate = function(event) {
    var urlFull = window.location.href,
        pageName = urlFull.split('/').pop();
    loadContentPage(pageName);
}

但是当我在与我的索引不同的 url 上刷新页面时,我仍然卡住了,my-app/studio 只返回我的视图内容,纯 html。 我认为这可能是一个路线问题,但我仍然不知道它

【问题讨论】:

  • 你考虑过使用 SPA 框架吗?
  • 当然,我想用 AngularJS 创建这个应用程序,但我想知道拥有 2 个具有相同规则 (MVC) 的框架(Laravel 5.1 和 Angular)是否是一个不错的选择。实际上我只使用 Laravel 5.1 和 jQuery。
  • 为什么要重新发明 ajax 导航轮?见:github.com/defunkt/jquery-pjax
  • 谢谢,看起来很酷,但是如果你看到我已经更新了我的问题,现在我的问题集中在 Laravel 的路线上,因为如果我刷新浏览器,我需要整个页面。实际上,我的路线返回的是视图的内容,而不是完整的页面,我需要以一种干净的方式配置我的路线,以便在刷新时获得完整的视图。

标签: jquery ajax laravel laravel-5.1 html5-history


【解决方案1】:

试试:

function loadContentPage(page){
var url = "{{ url('/') }}/";
    $('#content-wrapper').children().fadeOut(150, function(){
     if(page != 'index') { 
       url += page;
      } //end if you have other params update the url based on your routes
        $('#content-wrapper').load(url, function(data){
            $(this).html(data);
        });
    });
}

【讨论】:

  • 函数loadContentPage 只需要一个参数:route(这就是我从data-route 得到的),page 参数是什么意思?
  • 和我朋友的结果一样,我只得到视图内容 (html) :(
  • 你可以隐藏索引页面的内容,当用户点击主页链接时你只显示它而不做ajax,你保存一个http请求和带宽
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多