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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24568051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:46:09  来源:igfitidea点击:

Laravel: Get 405 Method Not Allowed while posting data using jquery ajax api to store data

phpjquerylaravellaravel-4

提问by star18bit

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

我正在尝试在 Laravel 4.* 中使用 jQuery ajax api 将数据保存到数据库并收到 405 错误。

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/

传递令牌:http: //words.weareloring.com/development/laravel/laravel-4-csrf-tokens-when-using-jquerys-ajax/

回答by Goddard

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');

回答by Rbex

Example in AJAX.

AJAX 中的示例。

var formData = new FormData();
            formData.append("password", password.val());
        $.ajax({
             url: 'btn',
             method: 'post',
             processData: false,
             contentType: false,
             cache: false,
             dataType: 'json',
             data: formData,
             success: function(data){
                    var vote_now = $("#vote_now");
                    if(data.btn == 'visible'){
                        vote_now.fadeIn();

                    }
                    else {

                        vote_now.fadeOut();
                    }

                    },
                error: function(){

             }
        });