在ajax响应中去除引号
在ajax响应中去除引号
响应返回为\"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); } } }
admin 更改状态以发布 2023年5月21日
这个数据并不是真正的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);