0

I am trying to send data with Javascript fetch and I keep getting the error: "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)".

inside 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 )
                    
                    })

and then in 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)

Although both print() commands display valid fetch data, the error relates to the line data_of_post = json.loads (request.body) is also shown. How to get rid of this error?

1 Answer 1

1

I think the issue is in the last line as ‘ description_save_result’ isn’t defined in code

Sign up to request clarification or add additional context in comments.

2 Comments

I changed it, but it's not a solution. As I wrote earlier: error page and console specifically point to the line data_of_post = json.loads (request.body) and the error Expecting value: line 1 column 1 (char 0).
What is the value of request.body?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.