最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

i was making an emerce 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);

i was making an emerce 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);
Share Improve this question edited May 23, 2022 at 11:42 PTomasz 1,7281 gold badge9 silver badges28 bronze badges asked May 23, 2022 at 10:29 Naimur SharonNaimur Sharon 911 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论