Problem with JS / JQuery / JSON / AJAX

1

I'm having some problems with JSON in my application and wanted to see if you guys can help me, please.

The situation is as follows:

I have a method in my controller that looks for some items:

@RequestMapping(value = "/buscaTodosCardapios", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String buscaTodosCardapios(Usuario usuario, Model model){
    JSONObject retorno = new JSONObject();
    try{        
        retorno.put("data", cardapioRepository.findBySubmenuIsNull());
    }catch (Exception ex){
        retorno.put("situacao", "ERRO");
        retorno.put("mensagem", "Falha ao iniciar produção!");
    }
    return retorno.toString();
}

A JS file that takes these items:

function buscaTodosCardapios(){
$.ajax({
    url: "buscaTodosCardapios",
    type: 'GET',
    dataType: 'json',
    cache: false,
    success: updateCardapios
});
}

function updateCardapios(data){
    console.log(data);
    console.log(data.cardapioId);
}

Return all OK:

Only, as you can see, the second console.log, does not return anything and I can not seem to get it to pick up some element from within the date. Can anyone help me with this?

-EDIT- Solution for anyone who needs it, just move to:

console.log(data.data[0].cardapioId);
    
asked by anonymous 23.01.2017 / 17:56

3 answers

2

You are returning an array.

Use This

console.log(data.data[0].cardapioId);
    
23.01.2017 / 17:58
0

Since you are bringing an array of objects, to gain access, it will have to be:

console.log(data.data[0].atributo);
    
23.01.2017 / 18:01
0

Hello, when you use @RequestMapping in the statement produces = {"application / json"} you do not have to jot down with @RespondeBody. You also do not need the return to be string type, you can directly return the object and Spring will transform it into json format.

    
23.01.2017 / 22:41