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
1 Answer
Reset to default 4This 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.