Данные запроса Django POST от Javascript fetch отображаются, но я получаю ошибку "Expecting value: line 1 column 1 (char 0)".

Я пытаюсь отправить данные с помощью Javascript fetch и продолжаю получать ошибку: "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)".

внутри index.js:

fetch('save_user_post/', {
                        method: 'POST',
                        credentials: 'same-origin',
                        headers:{
                            'Accept': 'application/json',
                            'X-CSRFToken': user_post_form_csrf.value,
                    },
                        body: JSON.stringify({
                            'input_user_post_value': input_user_post.value,
                    }) 
                    })
                    .then(response => {
                        
                            return response.json()
                    })
                    .then(user_post_save_result => {

                        console.log(user_post_save_result )
                    
                    })

и затем в views.py:

def save_user_post(request):
    if request.method != "POST":
            return JsonResponse({"error": "no POST method"}, status=400)
    if request.user.is_authenticated:
        print(request.body) #here I checked if I can receive this data
        data_of_post = json.loads(request.body)
        input_user_post_value = data_of_post.get('input_user_post_value', '')
        print(f'input_user_post_value is {input_user_post_value}') #here I checked if I can get exactly this value
        post_save_result = {'post_saved_or_not': 'saved'}
        return JsonResponse(post_save_result)

Хотя обе команды print() отображают действительные данные выборки, ошибка относится к строке data_of_post = json.loads (request.body). Как избавиться от этой ошибки?

Думаю, проблема в последней строке, поскольку ' description_save_result' не определен в коде

Вернуться на верх