Jquery 未能加载资源:http://localhost/laravel_crud/public/showPosts?page=1 服务器响应状态为500(内部服务器错误)
我在Laravel 5.3是新手,当我从DB加载这些页面时,我点击进入下一页,我得到一个无法加载数据的lert。请帮助我修复这些错误 BlogController.phpJquery 未能加载资源:http://localhost/laravel_crud/public/showPosts?page=1 服务器响应状态为500(内部服务器错误),jquery,ajax,laravel,pagination,Jquery,Ajax,Laravel,Pagination,我在Laravel 5.3是新手,当我从DB加载这些页面时,我点击进入下一页,我得到一个无法加载数据的lert。请帮助我修复这些错误 BlogController.php <?php namespace App\Http\Controllers; use App\Pots; use Illuminate\Support\Facades\Request; use App\Post; use DB; use View; class BlogController extends Control
<?php
namespace App\Http\Controllers;
use App\Pots;
use Illuminate\Support\Facades\Request;
use App\Post;
use DB;
use View;
class BlogController extends Controller
{
/**
* Posts
*
* @return void
*/
public function showPosts()
{
$posts = Post::paginate(1);
if (Request::ajax()) {
return Response::json(View::make('posts', array('posts' => $posts))->render());
}
return View::make('blog', array('posts' => $posts));
}
}
<body>
<h1>Posts</h1>
<div class="posts">
@foreach ($posts as $post)
<article>
{{ $post->title }}
</article>
@endforeach
{{ $posts->links() }}
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(window).on('hashchange', function() {
if (window.location.hash) {
var page = window.location.hash.replace('#', '');
if (page == Number.NaN || page <= 0) {
return false;
} else {
getPosts(page);
}
}
});
$(document).ready(function() {
$(document).on('click', '.pagination a', function (e) {
getPosts($(this).attr('href').split('page=')[1]);
e.preventDefault();
});
});
function getPosts(page) {
$.ajax({
url : '?page=' + page,
dataType: 'json',
}).done(function (data) {
$('.posts').html(data);
location.hash = page;
}).fail(function () {
alert('Posts could not be loaded.');
});
}
</script>
</body>
</html>
错误
如果您的路径中有另一个url,您的ajax找不到该页面,请使用以下url更改
showPosts
url : "{{url('showPosts')}}?page=" + page,
返回视图而不进行编码
public function showPosts()
{
$posts = Post::paginate(1);
return View::make('blog', array('posts' => $posts));
}
将输出更改为:
$('.posts').html($(data).find('.posts'));
如果您的路径中有另一个url,您的ajax找不到该页面,请使用以下url更改
showPosts
url : "{{url('showPosts')}}?page=" + page,
返回视图而不进行编码
public function showPosts()
{
$posts = Post::paginate(1);
return View::make('blog', array('posts' => $posts));
}
将输出更改为:
$('.posts').html($(data).find('.posts'));
我已经添加了有问题的路由文件,请查看并解释您的答案请@madalin ivascuyes我有jquery.min.js:6 GET 500(内部服务器错误)@madalin ivascuyes你在json响应中编码视图吗?我添加了有问题的routes文件,请查看并解释你的答案@madalin ivascuyes我有jquery.min.js:6 GET 500(内部服务器错误)@madalin ivascuyes你在json响应中编码视图吗?