简体   繁体   中英

JSON.parse: unexpected character at line 1 column 1 of the JSON data(django)

i was making an ecommerce website using django & trying to send data using fetch in javascript but this message is keep showing up. Tried 100 times to figure out what's the issue, but can't find one.

total = total bill ship = True means, shipping is necesarry because the product is not digital. form is a form where the user added their info

 var userFormData = {
      "name": "null",
      "email": "null",
      "total": total,
    };
    var shippingFormData = {
      "address": null,
      "city": null,
      "zipcode": null,
      "state": null,
    };
    if (user == "AnnonymousUser") {
      userFormData.name = form.name.value;
      userFormData.email = form.email.value;
    }
    if (ship == "True") {
      shippingFormData.address = form.address.value;
      shippingFormData.city = form.city.value;
      shippingFormData.zipcode = form.zipcode.value;
      shippingFormData.state = form.state.value;
    }
    console.log(userFormData);
    console.log(shippingFormData);
    var url = "/checkout_info_process/";
    fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-CSRFToken": csrftoken,
      },
      body:JSON.stringify({
        "userform": userFormData,
        "shippingform": shippingFormData,
      }),
    })
      .then((response) => response.json())
      .then((data) => {
        console.log(data);

This error occurs when you parse the response of your request. This means that your backend is not sending back a JSON. Try

.then((response) => response.text())

instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM