Get JSON value returned from Laravel

1

I have 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
}

But in javascript I can not 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"]);
});

follow the results:

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

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

    
asked by anonymous 29.08.2017 / 22:28

1 answer

2

For a simple details, you have missed the fourth heading which means the return type that in this case is json , like specified in the documentation , example :

  

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

ie the dataType can be:

  

xml, json, script, text e html

Changes were made to the example success function as well:

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

29.08.2017 / 23:20