首页 > 解决方案 > 获取 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)

标签: pythonjsondjango

解决方案


推荐阅读