dsfs64664 2014-07-04 06:32
浏览 270

Laravel:使用jquery ajax api发布数据时,获取405方法不允许存储数据

I am trying to save data to a database using jQuery ajax api in Laravel 4.* and receive a 405 error.

View

{{Form::open(array("","id"=>"frmProcessLevel"))}}
   <input name="{{$result->id.'_'.str_replace(' ','-',$title)}}" id="processLevel">

   <h3>{{$title}} Impact rating</h3>
   <table class="table table-bordered" style="font-size: 12px">
       <thead>
           <tr>
               <th>Level</th>
               <th>Category</th>
               <th>Description</th>
           </tr>
       </thead>
       <tbody class="selectable selectable-rows">
           @foreach($impact_rating as $key => $value)
                <tr data-key="{{$key}}">
                    <td>{{$key}}</td>
                    <td>{{$impact_cat[$key]}}</td>
                    <td>{{$value}}</td>
                </tr>
           @endforeach
       </tbody>
   </table>
   {{Form::token()}}
   {{Form::submit('Save')}}
{{Form::close()}}

<script>
        //Submit the form functions
        $("#frmProcessLevel").submit(function (e) {
            e.preventDefault();
            var selectedLevel = $("#processLevel").val();
            var datastring = 'selectedLevel='+selectedLevel;
            //alert(selectedLevel);
            $.ajax({ 
            headers: {
                    'X-CSRF-Token': $('meta[name="_token"]').attr('content')
                },
                url: "/bia/create_critical_process/",
                type: "post",
                data: datastring,
                success: function (data) {
                    console.log(data);
                }
            }, "json");
        });
</script>

Routes

Route::post('/bia/create_critical_process',array('before' => 'csrf','uses'=>'BiaController@createBiaStepThree'));

Controller

 public function createBiaStepThree(){
        $data = Input::all();
        $last_insert_bia_id = Session::get('last_insert_bia_id');
        if (Request::ajex())
        {
            Log::info(Input::all());
            $bia = new CriticalProcessStepThree();
            $bia->impact_rating_clinical = $data['selectedLevel'];
            $bia->key_process_fk = 1;
            $bia->bia_entry_fk = $last_insert_bia_id;
            $bia->save();
        }
    }

Passing token: http://words.weareloring.com/development/laravel/laravel-4-csrf-tokens-when-using-jquerys-ajax/

  • 写回答

2条回答 默认 最新

  • douyan8266 2015-07-22 23:00
    关注

    In your controller you have a typo.

    It should be

    if(Request::ajax()) 
    

    instead of

    Request::ajex()
    

    Not sure if this is your only problem, but that is one of them.

    EDIT : also you should set your request type.

    type    :"POST",
    

    Also make sure your route is setup to accept post requests like.

    Route::post('route/what/ever/you/want', 'Controller@function');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?