3
I am having the following problem, I need to take the Controller filtered objects to export to report. I’m sending by json using Ajax, but I don’t know if object is being sent to Controller,gave a console.log(Answer) and the following appears.
If it’s coming to the controller I don’t know how to get it, if I give decode_json error 500 follows the code:
JS
function ajaxExportar(colaboradores) {
var token = $("#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 to test for json.
public function exportar() {
$json = Request::input('json');
$colaborador=json_encode($json);
print_r($colaborador);
die();
}
Route:
Route::post('colaborador/exportar', 'Cadastro\ColaboradorController@exportar');
try the following:
json_decode($json, true);
in your controller and instead of die return the value and see in the "network" what value is receiving– Alvaro Alves
gave error 500 :jquery-1.10.2.min. js:4 POST http://localhost:8000/contributor/export 500 (Internal Server Error)
– Rafael Christ
and on the network gave : json_decode() expects Parameter 1 to be string, array Given
– Rafael Christ
right, what happens is that you are receiving the value of Request::input('json') as an array, not as a string
– Alvaro Alves
Give me a moment I’ll test it here
– Alvaro Alves
has an example of what you receive in the variable
colaboradores
? in Function ajaxExportar(collaborators)?– Alvaro Alves
another thing, which version of the Laravel you are using?
– Alvaro Alves
in the view ? onclick="ajaxExportar('{$collaborators}');" I get it from the controller to filter $contributors
– Rafael Christ
I’m using the Portable version 5
– Rafael Christ
Version 5.1,5.2 ..?
– Marcos Xavier
version 5.0
– Rafael Christ