H
H
Hadmi2019-07-10 15:11:19
JavaScript
Hadmi, 2019-07-10 15:11:19

What is the problem with indexing when passing a dictionary from django to jquery via json?

Good afternoon. I'm a little confused, please help.
In django, in view.py, I generate a multidimensional dictionary:

def get_actions_list(request):
    actions_list_dict = {}

    actions_list_dict['header'] = [
            {'title': 'Дата', 'anchor': 'start_date'},
            {'title': 'Оборудование', 'anchor': 'equipment'},
            {'title': 'Тип', 'anchor': 'type'},
            {'title': 'Вид', 'anchor': 'scheduled'},
            {'title': 'Контейнер', 'anchor': 'old_container'},
            {'title': 'Описание', 'anchor': 'description', 'tooltiped': True}
        ],

    actions_list_dict['table_params'] = {
            'html_id': 'actions_table',
            'html_class': 'content_table'
        },


I serialize to json and send via HttpResponse:

return HttpResponse(
        json.dumps({
            "result": actions_list_dict,
        }),
        content_type="application/json"
    )


On the client side, using jquery, I accept this data:

$.ajax({
        url: ajaxParams.url,
        type: 'POST',
        headers: {"X-CSRFToken": csrftoken},
        data: ajaxParams.data,
        success: function (json) {

                var table = json.result

        }, //end success


After that, I expect to get a result, for example,
table.header[0].title == 'Date'
or
table.header[3].title == 'View'

But in fact, another object is created that can be accessed only through the null header index, and to get the data I actually want I have to use this:
table.header[0][0].title == 'Date'
table.header[0][3].title == 'View '

Actually the question is - what kind of object is this, which you have to access through the index [0] and is it possible to get rid of it somehow?

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2019-07-10
@hadmi

And what for commas in places ], and }, ? As written, this is how it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question