python - 获取 json 解码器错误。但我无法弄清楚错误是什么?
问题描述
这是我在脚本标签中的 html 中的代码
document.getElementById('payment-info').addEventListener('click',function(e){
submitFormData()
})
function submitFormData(){
console.log('Payment Button Clicked')
var userFormData={
'name':null,
'email':null,
'total':total,
}
var shippingInfo={
'address':null,
'city':null,
'state':null,
'zipcode':null,
}
shippingInfo.address = form.address.value
shippingInfo.city = form.city.value
shippingInfo.state = form.state.value
shippingInfo.zipcode = form.zipcode.value
var url="/process_order/"
fetch(url, {
method:'POST',
headers:{
'Content-Type':'application/json',
'X-CSRFToken':csrftoken,
},
body:JSON.stringify({'form':userFormData,'shipping':shippingInfo}),
})
.then((response) => response.json())
.then((data) => {
console.log('Success:',data);
alert('Transaction Completed')
window.location.href="{% url 'index' %}"
})
}
这是我的views.py
def processOrder(request):
transaction_id=datetime.datetime.now().timestamp()
data = json.loads(request.body)
customer=request.user.customer
order, created=Order.objects.get_or_create(customer=customer,complete=False)
total=float(data['form']['total'])
order.transaction_id=transaction_id
if total == float(order.get_cart_total):
order.complete = True
order.save()
ShippingAddress.objects.create(
customer=customer,
order=order,
address=data['shipping']['address'],
city=data['shipping']['city'],
state=data['shipping']['state'],
zipcode=data['shipping']['zipcode'],
)
data=json.loads(request.body)
在我添加到邮政编码之前,它工作正常。当我添加那部分代码时,它开始向我显示此错误:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
在不添加该部分的情况下,我能够成功打印:
print('data:',request.body)
但添加该部分代码后,我收到此错误。我试图看看问题是什么,但已经过了一天,我无法找到问题所在。
任何帮助将不胜感激,谢谢
这是完整的错误:
Traceback (most recent call last):
File "c:\users\lenovo\appdata\local\programs\python\python38\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
During handling of the above exception (0), another exception occurred:
File "C:\Users\LENOVO\Envs\myapp\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\LENOVO\Envs\myapp\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\LENOVO\Envs\myapp\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\LENOVO\myapp\website\myapp\views.py", line 156, in processOrder
data = json.loads(request.body)
File "c:\users\lenovo\appdata\local\programs\python\python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "c:\users\lenovo\appdata\local\programs\python\python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\users\lenovo\appdata\local\programs\python\python38\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
Exception Type: JSONDecodeError at /process_order/
Exception Value: Expecting value: line 1 column 1 (char 0)
解决方案
推荐阅读
- python - 如何将每行分成两个字符串并在没有逗号的情况下打印?
- ios - 当方法参数为协议时,如何在 SwiftUI / Xcode 中轻松确定可以传递哪些类型?
- python-3.x - 对齐 QGridLayouts
- javascript - 设计游戏时的奇怪行为,猫消失了,游戏并没有停止
- keyboard-shortcuts - Firefox 开发者工具中关闭选项卡的快捷键
- android - 无法确定任务“:app:countFirebaseDebugDexMethods”的依赖关系
- javascript - 数据表中的排序图标问题
- swift - 无法以编程方式快速更改 UIBarItem 的颜色
- javascript - 画布:在不更改坐标的情况下使绘图适合画布大小
- python - 如何将 Python 解释器添加到 InteliJ?