I am getting JSONDecodeError in data=json.loads(request.body)
cart.js:
function updateUserOrder(productId, action) {
console.log('User is authenticated')
var url = '/update_item/'
fetch(url, {
method: 'POST',
headers: {
"Content-Type": 'application/json',
'X-CSRFToken': csrftoken,
},
body: JSON.stringify({'productId': productId, 'action': action})
})
.then((response) => {
return response.json()
})
.then((data) => {
console.log('Data: ', data)
location.reload()
});
}
In views.py:
def updateItem(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
print("Action:", action)
print("Product id", productId)
customer = request.user.customer
product = Product.objects.get(id=productId)
order, created = Order.objects.get_or_create(customer=customer, complete=False)
orderItem, created = OrderItem.objects.get_or_create(product=product, order=order)
if action == 'add':
orderItem.quantity += 1
elif action == 'remove':
orderItem.quantity -= 1
orderItem.save()
if orderItem.quantity <= 0:
orderItem.delete()
return JsonResponse('Item was added', safe=False)
I am getting this error.
JSONDecodeError at /update_item/
Expecting value: line 1 column 1 (char 0)
Request Method: GET
Request URL: http://127.0.0.1:8000/update_item/
Django Version: 3.1.3
Exception Type: JSONDecodeError
Exception Value:
Expecting value: line 1 column 1 (char 0)
Python version: Python 3.8.6
Django version: 3.1.3
I tried following other posts in stackoverflow and tried using cherrypy and various other ways for json.loads(request.body). Please resolve this error.
Edit:
Perhaps this may help:
Request information
USER
AnonymousUser
GET
No GET data
POST
No POST data
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…