Error in Json passage

Asked

Viewed 73 times

2

Good morning Guys,

I have an error reading my json array when I call it in my ajax.

My json:

[{"id":"7","nome":"Anderson","sobrenome":"Dorea"},{"id":"6","nome":"Diego ","sobrenome":"Andrade"},{"id":"8","nome":"Marcelo","sobrenome":"Cordova"}]

My ajax function:

$.ajax({
    url: 'package/getUser',
    type: 'POST',
    datatype: 'json',
    success: function(data)
    {
        for(i = 0; i < data.length; i++)
        {
            console.log(data[i]);

        }
    },
    error: function(data)
    {
        console.log(data)
    }
});

My controller:

function getUser()
{  

    $this->load->model('packageModel');
    $query = $this->packageModel->getUser()->result_array();

     echo json_encode($query);
}

It doesn’t pass array by array... it separates each letter when I put it into my for, but I need to play that information in a div with the append, only when I do so they come out as Undefined. Someone who can give me a light?

  • 1

    Try putting before the for: data = JSON.parse(data);

  • It worked, now why does this happen? I have other functions in this same capture pattern and did not need to do JSON.parse.

1 answer

1


The problem is that you used the wrong syntax on the line:

datatype: 'json',

The correct is camelCase:

dataType: 'json',

Since the syntax is wrong, the return comes as a normal string, and not as a JSON object that is parsed by dataType: 'json'.

  • 1

    Caracaaaa, I didn’t notice that! Great your analysis friend, congratulations.

Browser other questions tagged

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