Laravel 5:后期路由中的MethodNotAllowedHttpException以在服务器中保存数据但在localhost中正常工作

时间:2015-12-06 18:27:19

标签: ajax laravel-5

我正在尝试将表单数据保存到数据库。我的问题是我的代码在localhost中正常工作但在服务器中按下我的提交按钮时抛出了一个MethodNotAllowedHttpException。据我所知,如果我向GET Route发送POST请求,则会抛出MethodNotAllowedHttpException但是在我的POST请求中为什么我面对这个我不知道。有人说它可能导致CSRF令牌,但在我的表单中我发送令牌然后是什么问题。任何人请帮助我。我的代码如下:

路线:Route::post('/storequestion', 'QuestionController@storeQuestion');

用于处理表单数据的Ajax:

saveQuestion : function(e){

        e.preventDefault();
        // alert('text');

        var form = $(this);

        var method = form.find('input[name="_method"]').val() || 'POST';

        var url = form.prop('action');

        var dataString = form.serialize();

        // alert(method+url+dataString);

        $.ajax({

            type : method,

            url  : url,

            data : dataString 
        })
        .done(function(){

            form.trigger('reset');

            var message = form.data('remote-success');

            alert(message);


        })
        .fail(function(){

            alert("error");

        });


    }

形式:

{!! Form::open(['url'=>'/storequestion','data-remote'=>'data-remote','data-remote-success'=>'success']) !!}

{!! Form::hidden('fetch_category_table', '',['class' => 'fetch_category_table']) !!}

                {!! Form::hidden('fetch_category_id', '',['class' => 'fetch_category_id']) !!}

                <div class="form-group @if($errors->first('question')) has-error @endif">
                    {!! Form::label('question', 'Question') !!}
                    {!! Form::textarea('question', null, ['class' => 'form-control', 'placeholder' => 'Enter your Question']) !!}
                    <small class="text-danger">{{ $errors->first('question') }}</small>
                </div>

                <div class="form-group @if($errors->first('option1')) has-error @endif">
                    {!! Form::label('option1', 'First Option') !!}
                    {!! Form::text('option1', null, ['class' => 'form-control', 'placeholder' => 'Enter Option']) !!}
                    <small class="text-danger">{{ $errors->first('option1') }}</small>
                </div>

                <div class="form-group @if($errors->first('option2')) has-error @endif">
                    {!! Form::label('option2', 'Second Option') !!}
                    {!! Form::text('option2', null, ['class' => 'form-control', 'placeholder' => 'Enter Option']) !!}
                    <small class="text-danger">{{ $errors->first('option2') }}</small>
                </div>

                <div class="form-group @if($errors->first('option3')) has-error @endif">
                    {!! Form::label('option3', 'Third Option') !!}
                    {!! Form::text('option3', null, ['class' => 'form-control', 'placeholder' => 'Enter Option']) !!}
                    <small class="text-danger">{{ $errors->first('option3') }}</small>
                </div>

                <div class="form-group @if($errors->first('option4')) has-error @endif">
                    {!! Form::label('option4', 'Fourth Option') !!}
                    {!! Form::text('option4', null, ['class' => 'form-control', 'placeholder' => 'Enter Option']) !!}
                    <small class="text-danger">{{ $errors->first('option4') }}</small>
                </div>

                <div class="form-group @if($errors->first('option5')) has-error @endif">
                    {!! Form::label('option5', 'Fifth Option') !!}
                    {!! Form::text('option5', null, ['class' => 'form-control', 'placeholder' => 'Enter Option']) !!}
                    <small class="text-danger">{{ $errors->first('option5') }}</small>
                </div>

                <div class="form-group @if($errors->first('answer')) has-error @endif">
                    {!! Form::label('answer', 'Answer') !!}
                    {!! Form::text('answer', null, ['class' => 'form-control', 'required' => 'required', 'placeholder' => 'Enter Answer of the Question']) !!}
                    <small class="text-danger">{{ $errors->first('answer') }}</small>
                </div>

                <div class="form-group @if($errors->first('explanation')) has-error @endif">
                    {!! Form::label('explanation', 'Explanation') !!}
                    {!! Form::textarea('explanation', null, ['class' => 'form-control','placeholder' => 'Explanation of the Answer']) !!}
                    <small class="text-danger">{{ $errors->first('explanation') }}</small>
                </div>

                <div class="form-group @if($errors->first('direction')) has-error @endif">
                    {!! Form::label('direction', 'Direction') !!}
                    {!! Form::textarea('direction', null, ['class' => 'form-control','placeholder' => 'Any Clues of the Question']) !!}
                    <small class="text-danger">{{ $errors->first('direction') }}</small>
                </div>

            <div class="form-group">
                {!! Form::submit("Submit", ['class' => 'btn btn-primary']) !!}
            </div>
{!! Form::close() !!}

保存数据的控制器功能:

public function storeQuestion(Request $request) {

    $question = new Question;

    $question->question = htmlentities($request->question);

    $question->question_on_link_bar = $this->makeTheTitleLink(strip_tags($request->question));

    $question->option1 = $request->option1;

    $question->option2 = $request->option2;

    $question->option3 = $request->option3;

    $question->option4 = $request->option4;

    $question->option5 = $request->option5;

    $question->answer = $request->answer;

    $question->explanation = htmlentities($request->explanation);

    $question->direction = htmlentities($request->direction);

    $question->category_id = $request->fetch_category_id;

    $question->table_name = $request->fetch_category_table;

    $question->save();

}

0 个答案:

没有答案