404找不到错误Ajax Laravel 5.1

我试图在laravel 5.1的类别的基础上获得子类别,但首先它给了我一个'500内部服务器错误'的错误,但我包括

RewriteBase /

在我的.htaccess文件和nos它给我'错误'找不到404错误' . 我正在使用虚拟主机和Form facade,它自动包含'csrf token',所以我认为这不是'csrf'令牌问题的问题 . 任何人都可以在这方面提供帮助?????

这是我的路线档案:

// Admins Tasks

Route :: group(['middleware'=>'admin'],function(){

//Route::resource('/showcategoreis/{$id}', 'AdminHelperContooler@filterSubcat');
Route::get('/ajax-subcat/{id}', function(){
    return "mkj";
   $cat_id = Input::get('cat_id');
   $subcategories = Subcategory::where('category_id', '=', $cat_id)->get();

   return Response::json($subcategories);
});

Route::resource('/admin', 'Admin\AdminController');
Route::resource('/user', 'UserController');
Route::resource('/province', 'ProvinceController');
Route::resource('/city', 'CityController');

Route::resource('/category', 'CategoryController');


Route::resource('/subcategory', 'SubcategoryController');
Route::resource('/brand', 'BrandController');
Route::resource('/model', 'BModelController');
});

view.blade文件

@extends('admin.layout.master')

@section('title')
Add New Brand
@endsection

@section('content')
<div class="col-md-6 col-md-offset-3">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Add New Brand</h3>
        </div>
        <div class="panel-body">
            {!! Form::open(['files'=>true, 'url' => '']) !!}

                <div class="form-group">
                    {!! Form::label('brand', 'Brand Title') !!}
                    {!! Form::text('brand', null, ['class' => 'form-control']) !!}
                    <small class="text-danger">{{ $errors->first('brand') }}</small>
                </div> {{-- brand --}}

                <div class="form-group">
                     {!! Form::label('category_id', 'Category') !!}
                     {!! Form::select('category_id', $categories, $options = ['id'=>'category_id', 'class' => 'form-control']) !!}
                     <small class="text-danger">{{ $errors->first('category_id') }}</small>
                 </div> {{-- category_id --}}

                 <div class="form-group">
                     {{-- {!! Form::label('subcategory_id', 'Subcategory') !!} --}}
                     {{-- {!! Form::select('subcategory_id', $subcategories, $options = [ 'id'=>'subcategory', 'class' => 'form-control']) !!} --}}
                     {{-- <small class="text-danger">{{ $errors->first('subcategory_id') }}</small> --}}
                     <label for="">Subcategories</label>
                     <select name="subcategory" id="subcategory">
                        <option value=""></option>
                     </select>  
                 </div> {{-- subcategory_id --}}

                <hr>
                <div class="btn-group pull-left">
                    {!! Form::submit('Add', ['class' => 'btn btn-primary', 'style'=>'margin-right:5px']) !!}
                    {!! HTML::link(action('BrandController@index'), 'Cancel', ['class' => 'btn btn-warning']) !!}
                </div>

            {!! Form::close() !!}
        </div>
    </div>
</div>

@endsection

{{ - JSAX for AJAX - }}

<script type="text/javascript">
        $(document).ready(function(){
            $('#category_id').on('change', function(e){
                console.log(e);

                var cat_id = e.target.value;

                //Ajax
                $.ajax({
                    type: 'get',
                    // url: 'http://localhost/property-project/public/index.php/ajax-subcat?cat_id'+cat_id,
                    // url: 'http://www.laravelproperty.com/brand/create/ajax-subcat?cat_id='+cat_id,
                    url: '/ajax-subcat?cat_id='+cat_id,
                    scuccess: function(data){
                        console.log(data);
                    }
                });
                // $.get('/ajax-subcat?cat_id='+cat_id, function(data) {
                //  //scuccess data
                //  console.log(data);
                // });
            });
        });
    </script>

.htaccess文件

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

虚拟主机文件

<VirtualHost *:80>
    ServerAdmin m.khuramj@live.com
    ServerName laravelproperty.com
    ServerAlias www.laravelproperty.com

    DocumentRoot /var/www/property-project/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/property-project/public/>
        AllowOverride All
    </Directory>
</VirtualHost>

我已删除'.htaccess'中的更改但仍然收到错误,这是错误堆栈:

Sorry, the page you are looking for could not be found.

1/1
NotFoundHttpException in RouteCollection.php line 143:
in RouteCollection.php line 143
at RouteCollection->match(object(Request)) in Router.php line 746
at Router->findRoute(object(Request)) in Router.php line 655
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54

回答(1)

2 years ago

你定义的路线是 '/ajax-subcat/{id}' ,但你试图在你的AJAX中调用 '/ajax-subcat?cat_id='+cat_id . 将您的路线更改为 '/ajax-subcat' ,这至少应该修复您的404错误 .

或者,你可以改变你的AJAX来调用 '/ajax-subcat/'+cat_id