无法加载资源:http:// localhost / laravel_crud / public / showPosts?page = 1服务器响应状态为500(内部服务器错误)

时间:2016-09-23 05:11:54

标签: jquery ajax laravel pagination

我是Laravel 5.3的新手当我从数据库加载这些页面时,我点击进入下一页我得到一个数据无法加载的文件。请帮我解决这些错误

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

blog.blade.php

        <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>

Post.php (帖子模型)

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';

    /**
     * Define guarded columns
     *
     * @var array
     */
    protected $guarded = array('id');
}

路由

Route::get('/', function () {
    return view('insert');
});

Route::get("insert" , "testing@index");
Route::post("store" , "testing@store");

Route::get("showall" , "testing@showall");
Route::get("customer/{name}" , "testing@show");
Route::get("edit/{id}" , "testing@edit");
Route::patch("edit/update/{id}" , "testing@update");

Route::get("delete/{id}" , "testing@destroy");

Route::get("showPosts" , "BlogController@showPosts");

错误

It shows an alert post cant be loaded and the console error as shown

1 个答案:

答案 0 :(得分:0)

如果您的路线中有其他网址,则您的ajax无法使用以下网址更改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'));
相关问题