'<', "<!DOCTYPE "... is not valid JSON Promise.then (async) error in Django [closed]
I m currently learning Django and i m trying to build an ecommerce website. I m trying to figure out why am i getting this error in my console.I m trying to do a fetch call and trying to send data to the url by name of update_item and i m expecting the view function to return the json response in my console but i m constantly getting this error. Can someone pls help.
**This is my js file. **
var updateBtns = document.getElementsByClassName('update-cart')
for (i = 0; i < updateBtns.length; i++) {
updateBtns[i].addEventListener('click', function(){
var productId = this.dataset.product
var action = this.dataset.action
console.log('productId:', productId, 'Action:', action)
console.log('USER:', user)
if (user == 'AnonymousUser'){
console.log('User is not authenticated')
}else{
updateUserOrder(productId, action)
}
})
}
function updateUserOrder(productId, action){
console.log('User is authenticated, sending data...')
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)
});
}
This is my views.py file.
from django.shortcuts import render
from django.http import JsonResponse
from .models import *
# Create your views here.
def store(request):
products = Product.objects.all()
context = {'products':products}
return render(request,'store/store.html',context)
def cart(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
else:
# Create empty cart for now for non-logged in user
items = []
order = {'get_cart_total':0, 'get_cart_items':0 }
context = {'items': items, 'order':order}
return render(request, 'store/cart.html', context)
def checkout(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
else:
# Create empty cart for now for non-logged in user
items = []
order = {'get_cart_total':0, 'get_cart_items':0 }
context = {'items': items, 'order':order}
return render(request,'store/checkout.html',context)
def updateItem(request):
return JsonResponse('Item was added', safe=False)
This is my urls.py file
from django.urls import path
from . import views
urlpatterns = [
path('', views.store, name = "store"),
path('cart/', views.cart, name = "cart"),
path('checkout/', views.checkout, name = "checkout"),
path('update_item/', views.updateItem, name = "update_item")
]