getJSON with parsererror

0

I am attempting to perform the deletion and an item, so in controller I send a code to view 0 or 1 , but the JS function responsible for dealing this return is always falling in fail with the following error.

  

error [object Object] parsererror

Controller

if ($this->matricula_model->excluir($matricula) === true)
   echo json_encode(['codigo' => '0']);
else
   echo json_encode(['codigo' => '1']);

If the return is true (there was no deletion) it returns 0 , if not 1 . In getJSON I needed to capture this 0 and 1 to display a message, but I only get the error.

getJSON

$.getJSON($('#base-url').val() + '/matricula/excluir/' + sf, function(data) {
   console.log( "success", data );
  })
 .fail(function(textStatus, errorThrown) {
   console.log("error " + textStatus, errorThrown);
})
    
asked by anonymous 13.12.2018 / 19:04

2 answers

0

Hello, you can try the following:

if ($this->matricula_model->excluir($matricula) === true)
 echo json_encode(array('codigo' => '0'));
else
 echo json_encode(array('codigo' => '1'));

or

if ($this->matricula_model->excluir($matricula) === true)
 echo json_encode((object)['codigo' => '0']);
else
 echo json_encode((object)['codigo' => '1']);

array () or (object) respectively serve to force this JSON to be an array or object.

I hope it helps: D

    
13.12.2018 / 20:45
0
$.getJSON($('#base-url').val() + '/matricula/excluir/' + sf, function(data) {

   // tente modificar inserir isso antes do console.log
   var data = JSON.parse(data);
   console.log( "success", data );

 })

 .fail(function(textStatus, errorThrown) {
   console.log("error " + textStatus, errorThrown);
})
    
13.12.2018 / 20:07