0
Hi, I have a problem with the query answer I made to db is full of \n
and \
. Does anyone know what the problem might be? Thank you.
model function, query a db in search of products added to cart by a certain user
function _get_cart_by_user($id)
{
$this->db->where('user_id',$id);
$query = $this->db->get('cart');
return $query;
}
controller function, only receives the model result and does re-turn from it
public function get_cart()
{
$id=$this->input->post('user_id', TRUE);
$this->load->model('cart_model');
$query=$this->cart_model->_get_cart_by_user($id);
$result= $query->result();
var_dump($result);
}
AJAX request, which should receive the return of the controller and show in the browser console
function addItemToCart(user_id) {
$.ajax({
url: BASE_URL + 'cart/get_cart',
type: 'POST',
data: {
'user_id': user_id
},
success: function (data) {
console.log(JSON.stringify(data))
},
error: function () {
alert("error");
}
});
this is the result of var_dump
of the controller.
"array(2) { n [0]=> n >Object(stdClass)#24 (6) { n >[ "id "]=> n string(2) > "16 " n [ "user_id "]=> n >string(1) "5 " n
[\"item_id "]=> n >string(1) "2 " n
[\"title "]=> n string(4) > "Buddha " n [ "price "]=> n >string(4) "9.99 " n
[\"image "]=> n string(0) > " n } n [1]=> n >Object(stdClass)#25 (6) { n >[ "id "]=> n string(2) > "17 " n [ "user_id "]=> n >string(1) "5 " n
[\"item_id "]=> n >string(1) "3 " n
[\"title "]=> n string(12) > "budaoriginal " n
[\"price "]=> n string(4) > "9.99 " n [ "image "]=> n >string(0) " " n } n} n"
Can anyone tell me what these are \
and \n
, I also tried to make json_enconde
(did echo of json_encode
and appears the \
) and move to AJAX but when I do the Response console it just appears "".
can use a Regex to remove the escape characters
\n
– Ricardo Pontual