Level 88
Normally you get this exception when the csrf token is not correct or missing. Do you have a meta tag with the token?
Documentation: https://laravel.com/docs/5.6/csrf#csrf-x-csrf-token
2 likes
I am facing "exception": "Symfony\Component\HttpKernel\Exception\HttpException" in my laravel file, here is my .ajax-jquery script:
$('#companyyear_form').on('submit', function(event){
event.preventDefault();
var company = $('#company').val();
var checkbox_Names = new Array();
$("input[name='month_year_multi_id[]']").each(function(){
if($(this).is(':checked')&& $(this).is(':enabled')) {
checkbox_Names.push($(this).val());
}
});
console.log(checkbox_Names);
console.log(company);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
},
url: 'postcompanyyear',
method:'POST',
data: {
company:company,
checkbox_Names:checkbox_Names,
},
dataType: 'json',
success: function(data)
{
console.log(data);
$('#form_output').html(data.success);
$('#companyyear_form')[0].reset();
$('#action').val('Add');
$('.modal-title').text('Add Program');
$('#button_action').val('insert');
},
error:function(data)
{
console.log(data);
}
})
});
and here is my controller:
public function postdata(Request $request)
{
$month_year_multi_ids = $request->get('checkbox_Names');
foreach ($month_year_multi_ids as $key => $value) {
$last_space = strrpos($value, ' ');
$year[] = substr($value, $last_space);
$month[] = substr($value, 0, $last_space);
}
$validation = $request->validate([
'company' => 'required',
'checkbox_Names' => 'required',
]);
$error_array = array();
$success_output = '';
if ($request->get('button_action') == "insert")
{
foreach ($month_year_multi_ids as $index => $values) {
$company_year = new CompanyYear([
'company_id' => $request->get('company'),
'year' => $year[$index],
'month' => $month[$index],
]);
$company_year->save();
}
}
$success_output = '<div class="alert alert-success">Data Inserted</div>';
$output = array(
'errror' => $error_array,
'success' => $success_output
);
echo json_encode($output);
}
and this is my postcompanyyear route:
Route::post('postcompanyyear','CompanyYearController@postdata');
may be the data is not passing, can you tell me, why I am getting the exception?? and also the solution.
Normally you get this exception when the csrf token is not correct or missing. Do you have a meta tag with the token?
Documentation: https://laravel.com/docs/5.6/csrf#csrf-x-csrf-token
Please or to participate in this conversation.