getJSON with parsererror

Asked

Viewed 105 times

0

I am trying to realize the deletion and an item for this controller send a code to view 0 or 1, but the JS function responsible for treating that return is always falling into the 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) returns 0, if not 1. In getJSON I needed to capture this 0 and 1 to show a message, but 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);
})
  • 1

    Added the header? header('Content-Type: application/json');

  • Yes, it’s already set up.

2 answers

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);
})
  • Unsuccessfully, you continue with the same mistake.

  • He falls right into the fail. Unsuccessful.

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 also

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

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

hope it helps :D

  • Unfortunately without success with both.

  • hmm, have you tried to see how your JSON is being created? to see if it is being created correctly?

  • The following format. {"code":"1"}

  • $('#base-url'). val() + '/matricula/delete/' + sf está would be your correct url? can you access it to see what it returns? ever tried to use only $.get()?

  • Yes is the correct URL, it will return me an error but the error is DELETE that cannot be done pq there are column references in another table.

  • But in case of this error it should return me the code 1 for me to inform the user that there was an error in the deletion.

  • I think Voce already tried to create the JSON and assign in a variable and then send it to echo ? $var = (Object) ["Cad" => 1]; echo json_encode($var);

  • Yes I had tried, so it works kkk

  • kkkk Bah what a thing

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.