[英]remove quotation mark in ajax response
響應返回為"4"
而不是4
我嘗試將其更改為 .done(function(data)) 但仍然有相同的結果
$.ajax({
url: "../api/ajax/addToCart.php",
type: "post",
data: data
})
.done(function(response) {
// alert(response);
$('#cart_counter').html(response);
// console.log(JSON.parse(response));
getCart();
// console.log(response);
});
ajax 正在從這個頁面 addToCart.php 獲取響應
$sql1 = 'DELETE FROM temp_cart WHERE item_id = "' . $item_id . '" AND temp_id = "' . $temp_id . '"';
$result = $conn->query($sql1);
{
$sql2 = 'INSERT INTO temp_cart(temp_id, temp_name, temp_number, item_name, item_price, item_quantity, item_total, item_pic, item_id, date_expiry) VALUES ("' . $temp_id . '", "' . $temp_name . '", "' . $temp_number . '", "' . $item_name . '", "' . $item_price . '", "' . $item_quantity . '", "' . $total_row . '", "' . $item_pic . '", "' . $item_id . '", "' . $date_expiry . '" )';
$result = $conn->query($sql2);
{
$sql = "SELECT count(item_quantity) as count_quantity FROM temp_cart WHERE temp_id='$temp_id'";
$resultb = $conn->query($sql);
while($rowb = $resultb->fetch_assoc())
{
$cart_counter=$rowb['count_quantity'];
echo json_encode($cart_counter);
}
}
}
數據並不是真正的 JSON 格式,而是當您將其作為 JSON 傳回時被字符串化的數字,因此它以字符串結尾。 只需根據需要將字符串解析為數字:
$('#cart_counter').html(parseInt(response));
let counter = 4; let json = JSON.stringify(counter); console.log(json, `is a ${typeof json}`); console.log(`...now a ${typeof parseInt(json)}`); document.querySelector('#target').innerHTML = parseInt(json);
<div id="target"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.