Laravel中RouteCollection.php第218行的MethodNotAllowedHttpException

时间:2017-05-13 06:37:19

标签: php laravel

我想在Laravel中使用post方法,但它抛出异常:RouteCollection.php第218行中的MethodNotAllowedHttpException

以下是完整错误:

in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 766
at Router->findRoute(object(Request)) in Router.php line 621
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 53
at require_once('E:\Laravel\myproject\public\index.php') in server.php line 21  

这是我的表单代码:

<div id="education_form">
{{ Form::open(array('method' => 'post','data-parsley-validate')) }}
{{ Form::hidden('user_id', Auth::user()->id, array('id' => 'user_id')) }}


<div class="form-group">
    {{Form::label('school','School')}}<span class="required">*</span>
    {{Form::text('school',null,['class'=>'form-
    control','id'=>'school','placeholder'=>'Enter School Name'])}}
</div>
<div class="form-group">
    {{Form::label('degree','Degree')}}<span class="required">*</span>
    {{Form::text('degree',null,['class'=>'form-
    control','id'=>'degree','placeholder'=>'Enter Degree'])}}
</div>

<div class="form-group">
    {{Form::label('specialization','Specialization')}}
    {{Form::text('specialization',null,['class'=>'form-
    control','id'=>'specialization','placeholder'=>'Enter 
    Specialization'])}}
</div>
{{Form::submit('Save',array('id'=>'saveEducation','class'=>'form-submit btn 
btn-success btn-md pull-right'))}}

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

我使用url将数据传递给ajax,如下所示,这就是为什么表单标签中没有url。

<script>
var urlSave = '{{ route('save') }}';
</script>

这是我的路线档案:

Route::post('/save', [
'uses' => 'MyController@save',
'as' => 'save'
]); 

请让我知道我在做什么错误...谢谢!

2 个答案:

答案 0 :(得分:0)

单引号在单词save之前结束,并在save it cause

之后再次启动
  

未捕获的SyntaxError:意外的标识符

var urlSave = "{{ route('save') }}";

答案 1 :(得分:0)

你应该试试这个:

$.ajax({
    url : "{!! route('save') !!}",
    type: "post",
    data : {data:hierarchy.nestable('serialize')},
    success: function(data) {
        //Proceed to success
    },
    error: function (jqXHR, textStatus, errorThrown) {
        //Check for error
    }
});

OR以下列形式传递操作方法:

{{ Form::open(array('route' => 'route','method' => 'post','data-parsley-validate')) }}