laravel错误MethodNotAllowedHttpException没有消息

时间:2018-08-05 09:31:14

标签: jquery laravel

我得到MethodNotAllowedHttpException我的laravel程序中没有消息错误。在检查代码元素时,控制台表示错误在jquery.min.js

这是我的观点:

<div class="container">
          <div class="row">
              <div class="col-sm-10 col-sm-offset-2 col-md-10 col-md-offset-1">
                <div class="misc-box" style="min-height: 400px">

                </div>

                <div class="text-center misc-footer">
                    <span>Copyright &copy; {{ $name }}<br>E-SKM 1.0 - 2016</span>
                </div>
              </div>
          </div>
        </div>
      </div>
<!-- Survey Sections -->
  <script src="{{asset('js/survey_sections.js')}}"></script>
  <script>
      $(document).ready(function () {
          setPeriode('{{$selected}}');
      });

      function setPeriode(val) {
        $('.misc-box').html('');
        $.post('{{url("survey/getSurvey")}}', {_token: "{{ csrf_token() }}", p_id: val}, function(result) {
          $('.misc-box').html(result);
        });
      }
  </script>

这是我的路线:

Route::post('/survey/getSurvey', 'SurveyController@getSurvey');

和我的控制器:

public function getSurvey(Request $request)
    {
        try{
            $input = $request->all();
            $p_id = $input['p_id'];    
            $periode = IKMPeriode::find($p_id);
            if ($periode) {
                $periode->update(['statactive' => 1]);
            }
            $operiode = IKMPeriode::where('id', '<>', $p_id)->get();
            foreach ($operiode as $key => $op) {
                $op->update(['statactive' => 0]);
            }

            $quest = IKMKuisioner::where('i_k_m_periode_id', $p_id)->get();

            $view = View::make('survey.survey_sections', compact('quest', 'periode'));
            return $view->render();
        }catch(\Exception $e){
            throw $e;
        }
    }

有人可以帮助我吗?请:)

2 个答案:

答案 0 :(得分:0)

如laravel文档X-CSRF-TOKEN

中所述

向页面标题添加元令牌

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

并将此代码添加到脚本中

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

现在您可以从jquery $ .post函数中删除_token参数

答案 1 :(得分:0)

您可以尝试以下代码。似乎您的csrf令牌未发送

<script type="text/javascript">
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
$.ajax({
    type: 'POST',
    url: '/survey/getSurvey',
    data: { p_id: val} ,
    success: function(data){
        console.log(data);
    }
});

</script>