Laravel AJAX根本不起作用

时间:2016-02-13 22:24:35

标签: ajax laravel

所以,尝试了10多种方法来制作一个带有laravel的简单ajax形式,但它们都没有正常工作。 任何人都有一个适用于最后5.2 laravel框架的工作示例。

感谢

文件(routes.php):

Route::group(['middleware' => ['web']], function () {
    Route::get('/', 'CategoryController@index');
    Route::get('/ajaxsubcat/{id}', 'CategoryController@cats');
});

文件(CategoryController.php):

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
use App\Subcategory;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CategoryController extends Controller
{
    public function index()
    {
        $categories = Category::all();
        return view('index', ['categories' => $categories]);
    }
    public function cats(Request $request, $id)
    {
        $cat_id = $id;
        $subcategories = Subcategory::where('category_id', '=', $id)->get();
        return Response::json($subcategories);
    }
}

文件(index.blade.php):

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Categories</title>
    <script src="{{asset('js/jquery.min.js')}}"></script>
</head>
<body>
    <div class="container">
        <h1>Cats & Subs</h1>
        <div class="col-md-4">
            {{ Form::open(['url'=>'', 'files'=>'true']) }}
                <div class="form-group">
                    <label for="">cats</label>
                    <select class="form-control input-sm" id="category" name="category">
                        @foreach($categories as $category)
                        <option value="{{$category->id}}">{{$category->name}}</option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label for="">subs</label>
                    <select class="form-control input-sm" name="subcategory" id="subcategory">
                        <option value=""></option>
                    </select>
                </div>
            {{ Form::close() }}
        </div>
    </div>
<script>
    $('#category').on('change',function(e){
        console.log(e);
        var cat_id = e.target.value;
        //ajax
        $.get('/ajaxsubcat/'+cat_id, function(data){
            //success data
            $('#subcategory').empty();
            $.each(data, function(index, subcatObj){
                $('#subcategory').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');
            });
        });
    });
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您应该尝试将X-CSRF令牌添加到您的请求中。为此,请将以下行添加到html中的<head></head>标记中:

<meta name="csrf-token" content="{{ csrf_token() }}">

在你的ajax请求中:

var request = $.ajax({
    url: '/ajaxsubcat/' + cat_id,
    method: 'get',
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

request.success(function(data) {
    //success data
    $('#subcategory').empty();
    $.each(data, function(index, subcatObj){
        $('#subcategory').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');
    });
});

以下是该文档:https://laravel.com/docs/5.2/routing#csrf-protection