I've been encountering an issue while attempting to transmit data using Javascript fetch. The error message I keep getting is: "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"
Within 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 )
})
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) #I checked here for successful reception of 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 confirmed that the correct value was obtained
post_save_result = {'post_saved_or_not': 'saved'}
return JsonResponse(post_save_result)
Despite confirming valid fetch data with both print() commands, the error persists specifically within the line data_of_post = json.loads(request.body). Any suggestions on how to resolve this issue?