I'm having the following problem, I need to get the filtered objects in the Controller to export to report. I'm sending json using Ajax, but I do not know if the object is being sent to Controller, I gave a console.log (response) and the following appears.
Ifyou'regettingintothecontroller,Idonotknowhowtogetit,ifIgivedecode_jsonoferror500followthecode:
JS
functionajaxExportar(colaboradores){vartoken=$("#token").val();
var json = stringToJson(colaboradores);
$.ajax({
url: '/colaborador/exportar',
type: 'POST',
method: 'POST',
dataType: 'json',
ContentType: 'application/json; charset=utf-8',
headers: {
'X-CSRF-TOKEN': token
},
data: {
'json': json,
'_token': token
},
beforeSend: function() {
$('a').addClass('disabled');
$("#overlay").faLoading({
"type": "add",
"icon": "fa-refresh",
"status": 'loading',
"text": false,
"title": ""
});
},
success: function(response) {
console.log(response);
var json = (response);
$("#overlay").faLoading("remove");
$('a').removeClass('disabled');
if (json.error) {
new PNotify({
title: 'Erro!',
text: json.error,
icon: 'fa fa-warning',
type: 'error',
styling: 'fontawesome'
});
}
},
statusCode: {
//erro de autenticação em caso de logout
401: function() {
alert("Necessário fazer login novamente!");
window.location = "/home";
// window.location.reload();
},
//erro de servidor
500: function() {
alert("Erro servidor");
},
//not found
404: function() {
alert("Arquivo não encontado");
}
}
})
}
Controller
controller under test to get json.
public function exportar() {
$json = Request::input('json');
$colaborador=json_encode($json);
print_r($colaborador);
die();
}
Route :
Route::post('colaborador/exportar', 'Cadastro\ColaboradorController@exportar');