Pick JSON value returned from Laravel

Asked

Viewed 303 times

1

I got the following JSON returned from a controller Laravel:

{
  "id": 105,
  "tratamento_id": "24",
  "eim": null,
  "oft": "12",
  "codigo_produto": "CO009-1200-1200",
  "descricao_produto": "COMPENSADO 9X1200X1200  ",
  "tipo_mercadoria": "2",
  "comprimento": "2",
  "largura": "2",
  "espessura": "2",
  "quantidade": ".0000",
  "m3_unitario": "2.0000",
  "m3_total": "2.0000",
  "estufa": "0",
  "nfe": "2",
  "kit": "1",
  "kit_id": null
}

however in javascript I can’t get the value of id, follow the code with the tests I did:

$.post(URL+'/json/remover_item_kit',{
    _token: $('input[name=_token]').val(),
    id: $("#id_item_kit_delete").val()
}).done(function(data){

    console.log("1 :" + data.id);
    console.log("2 :" + data[0].id);
    console.log("3 :" + data["id"]);
    console.log("4 :" + data[0]["id"]);
});

follows the results:

1 :undefined
2 :undefined
3 :undefined
4 :undefined

I needed to access the date properties but did not succeed.

  • What gives console.log(typeof data, data);?

1 answer

2


For a simple detail, we missed passing the room Paramento that means the type of return that in the case is a json, as specified in the documentation, example:

jQuery.post( url [, data ] [, success ] [, dataType ] )

i.e., the dataType can be:

xml, json, script, text e html

The changes were made including the receipt of the success function, example:

$.post(URL+'/json/remover_item_kit',
  {
       _token: $('input[name=_token]').val(), 
       id: $("#id_item_kit_delete").val()
  }, 
  function(data)
  {
      console.log(data.id);
  }, 'json');

References:

  • 1

    Ball show worked, also found another solution by adding this start of the function done data = JSON.parse(date);

Browser other questions tagged

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