3

I'm trying to parse some data from my ERP platform (API written in C#). When I try to parse the info contained in data, I get an undefined result. I've been looking for some answers and I even could figure out an alternative way to do it, with this:

for(var  i in data){

      top[i] = $.parseJSON(JSON.stringify(data[i]));

But I'd like to know why this JSON parse isn't working since I'm doing as demanded.

My web app API is written in Ruby on Rails, FYI

Best regards,

.js file(home.js)

$(document).ready(function () {

    $('#artigos-container').html(botao_load);

    var id_categoria = $("#titulo-categoria").attr('data-id-categoria');
    console.log("ID CATEGORIA : " + id_categoria);
    var url_categoria = base_url_primavera + '/categorias/'+id_categoria;

    /*var id_subcategoria = null;
    var url_subcategoria = null;
    var subcategoria = $("#titulo-subcategoria");
    if(subcategoria.length){
        is_subcategoria = true;
        id_subcategoria = subcategoria.attr('data-id-subcategoria');
        url_subcategoria = base_url_primavera + '/categorias/'+id_categoria+'/subcategoria/' + id_subcategoria;
    }*/

    $.ajax({
        type: 'GET',
        url: url_categoria,
        error: function (err) {
            console.log("error fetching category");
            $("#titulo-categoria").html('erro');
        },
        dataType: 'json',
        success: function (data) {
            console.log(data);
            var categoria_temp = $.parseJSON(data);
            var desc_categoria = categoria_temp.DescCategoria;
            console.log(desc_categoria);
            $("#titulo-categoria").html(desc_categoria);
        }
    })
});

The data variable log is

{"CodCategoria":"H01","DescCategoria":"Computadores","numExemplaresCategoria":0}
7
  • 1
    What exactly is the error you get? Commented Dec 8, 2016 at 23:48
  • The result of that parse is undefined so the following errors I get are about it. The main question is why is that parse returning null or undefined. Commented Dec 8, 2016 at 23:50
  • 2
    $.parseJSON has been deprecated, you should use the built-in JSON.parse() Commented Dec 8, 2016 at 23:50
  • Since you wrote dataType: 'json', jQuery will automatically parse the JSON response, and sets data to the result. So when you call $.parseJSON(data) yourself, data is not a JSON string. Commented Dec 8, 2016 at 23:51
  • 1
    I'm suggesting var categoria_temp = data;, since data is already parsed. Commented Dec 8, 2016 at 23:52

1 Answer 1

4

As you have already mentioned dataType: 'json', jQuery will already parse the data. So you don't need to parse again.

So change this line var categoria_temp = $.parseJSON(data); to,

var categoria_temp = data;

Also when you do for..in as below, the value in data[i] is not a valid json to parse.

for(var  i in data){

      top[i] = $.parseJSON(JSON.stringify(data[i]));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.