async function cart() {
const count=document.querySelector(".readonly");
if(product_json.products.aroma_grade == 0){
const weight=1;
let formdata = new FormData
formdata.append('count', count.value)
formdata.append('price', String(product_json.products["price"]))
formdata.append('weight', weight)
const response = await fetch(`${BACK_END_URL}/product/cart/?product_id=${product_id}`, {
headers:{
"Authorization": "Bearer " + localStorage.getItem("access"),
},
method: "POST",
body: formdata
})
response_json=await response.json()
if (response.status==200 || response.status==202 || response.status == 201){
alert("장바구니에 담겼습니다.")
location.reload();
}
else if(response.status==401 || response.status == 400){
alert("로그인을 해주세요")
location.reload();
}}
else if(product_json.products.aroma_grade >= 1) {
const count=document.querySelector(".readonly");
const weight=document.querySelectorAll("select")[0];
if (weight.value ==0){
alert("용량을 선택해주세요")
}
if (weight.value > 1){
let formdata = new FormData
formdata.append('count', count.value)
formdata.append('price', String(product_json.products["price"]))
formdata.append('weight', weight.value)
const response = await fetch(`${BACK_END_URL}/product/cart/?product_id=${product_id}`, {
headers:{
"Authorization": "Bearer " + localStorage.getItem("access"),
},
method: "POST",
body: formdata
})
response_json=await response.json()
if (response.status==200 || response.status==202 || response.status == 201){
alert("장바구니에 담겼습니다.")
location.reload();
}
else if(response.status==401 || response.status == 400){
alert("로그인을 해주세요")
location.reload();
}
}
}
}
장바구니 담는코드
if문 한두번이면 해결될거같은데 const변수선언떄문에 통쨰로 했다.
'스파르타코딩개발일지' 카테고리의 다른 글
중간 KPT 회고록 (0) | 2022.12.15 |
---|---|
TIL (0) | 2022.12.13 |
TIL (0) | 2022.12.11 |
TIL (0) | 2022.12.11 |
TIL (0) | 2022.12.08 |