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/