从None引发JSONDecodeError(“期望值”,s,err.value)json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)

时间:2020-08-01 14:05:34

标签: python json django

我第三次遇到此错误。前两次是缺少逗号和拼写错误,但这一次我检查了所有内容并找不到错误原因。 这是我 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'],
        )

我的模型中有各自的字段。

我找不到错误。这是我得到的完整错误:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

将寻求任何帮助。

0 个答案:

没有答案