获取返回 500(内部服务器错误)JavaScript
Fetch returning 500(internal server error) JavaScript
我四处寻找,但似乎找不到有效的答案。
我正在关注电子商务网站的 Dennis Ivy 的 Django 教程,但我 运行 遇到一个问题,我试图将商品添加到我的购物车但没有任何反应,检查控制台我收到以下两个错误:
POST http://127.0.0.1:8000/update_item/ 500 (Internal Server Error)
updateUserOrder @ cart.js:26 (The fetch line)
(anonymous) @ cart.js:15 (when the function is called)
127.0.0.1/:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
omise.then (async)
updateUserOrder @ cart.js:39 (the second .then)
(anonymous) @ cart.js:15 (when the function is called)
这是我的 JavaScript cart.js updateUserOrder 函数:
function updateUserOrder(productId, action) {
console.log('User is logged in, 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) => {
location.reload()
});
}
这是我的观点updateItem
:
def updateItem(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
print('Action:', action)
print('Product:', 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(order=order, product=product)
if action == 'add':
orderItem.quantity = (orderItem.quantity + 1)
elif action == 'remove':
orderItem.quantity = (orderItem.quantity - 1)
orderItem.save()
if orderItem.quantity <= 0:
orderItem.delete()
return JsonResponse('Item was added', safe=False)
这是我的 URL 包括导入:
from checkout.views import updateItem
path('update_item/', updateItem, name="update_item"),
提前致谢!
关于第一个错误POST http://127.0.0.1:8000/update_item/ 500 (Internal Server Error)
,您需要检查服务器日志。
关于第二个错误 127.0.0.1/:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
,即使响应是 HTTP 500,从 fetch() 返回的 Promise 也不会拒绝 HTTP 错误状态。相反,它将正常解析(状态为 ok设置为假)。因此,您需要添加代码来检查 then()
中的响应,如下所示。
...
.then((response) => {
if (!response.ok) {
// error processing
throw 'Error';
}
return response.json()
})
...
在 location.reload() 中的 JavaScript 文件中
你不应该在花括号和圆括号后添加分号 })
我四处寻找,但似乎找不到有效的答案。 我正在关注电子商务网站的 Dennis Ivy 的 Django 教程,但我 运行 遇到一个问题,我试图将商品添加到我的购物车但没有任何反应,检查控制台我收到以下两个错误:
POST http://127.0.0.1:8000/update_item/ 500 (Internal Server Error)
updateUserOrder @ cart.js:26 (The fetch line)
(anonymous) @ cart.js:15 (when the function is called)
127.0.0.1/:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
omise.then (async)
updateUserOrder @ cart.js:39 (the second .then)
(anonymous) @ cart.js:15 (when the function is called)
这是我的 JavaScript cart.js updateUserOrder 函数:
function updateUserOrder(productId, action) {
console.log('User is logged in, 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) => {
location.reload()
});
}
这是我的观点updateItem
:
def updateItem(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
print('Action:', action)
print('Product:', 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(order=order, product=product)
if action == 'add':
orderItem.quantity = (orderItem.quantity + 1)
elif action == 'remove':
orderItem.quantity = (orderItem.quantity - 1)
orderItem.save()
if orderItem.quantity <= 0:
orderItem.delete()
return JsonResponse('Item was added', safe=False)
这是我的 URL 包括导入:
from checkout.views import updateItem
path('update_item/', updateItem, name="update_item"),
提前致谢!
关于第一个错误POST http://127.0.0.1:8000/update_item/ 500 (Internal Server Error)
,您需要检查服务器日志。
关于第二个错误 127.0.0.1/:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
,即使响应是 HTTP 500,从 fetch() 返回的 Promise 也不会拒绝 HTTP 错误状态。相反,它将正常解析(状态为 ok设置为假)。因此,您需要添加代码来检查 then()
中的响应,如下所示。
...
.then((response) => {
if (!response.ok) {
// error processing
throw 'Error';
}
return response.json()
})
...
在 location.reload() 中的 JavaScript 文件中 你不应该在花括号和圆括号后添加分号 })