L
L
Lolo Penguin2020-05-31 12:32:56
Django
Lolo Penguin, 2020-05-31 12:32:56

How to pass an array via json and return an array?

I have a task to transfer an array to a view, and from there back in an array.

it looks like this for me (does not work):

js

var arr_obj = [5, 7, 23, 99, 21, 29];    

$.ajax(
{
    type: "GET",
    url: "{% url 'likepost' %}",
    // dataType: 'html',
    // cache: false,  // запретит браузеру кешировать
    data : JSON.stringify({
        post_id: arr_obj
    }),
    dataType: "json",
    success: function(data) {
        alert(data)
    }
})


views
def likePost(request):

    if request.method == 'GET':
        post_id = request.GET['post_id']
        return HttpResponse(post_id)

    else:
        return HttpResponse("Is not a GET")

settings.py
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
)
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lolo Penvin, 2020-05-31
@ledo02

Loading Django content?

R
Roman Kitaev, 2020-05-31
@deliro

Read rfc on http and understand what you are doing garbage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question