Devolver objetos propios por Ajax
Publicado por giuli (19 intervenciones) el 22/07/2018 16:22:08
estoy desarrollando un sistema de ventas, y en la ventana de ticket tengo el boton buscar que permite ir a otra pagina con el listado de articulos. Este consta dfe dos botones. Uno es agregar que añade el producto en cuestion a un array de detalles del objeto ticket(trabajo con MVC y POO), y elotro un terminado que retorna a la ventana anterior
Clase ticket y detalle.
El problema que tengo es que quiero listar los articulos añadidos al carrito al retornar a la pagina y me da un error de parseo de JSON: REQUESTED JSON PARSE FAILED.
Para lo mismo tengo un codigo jquery intermedio y un archivo de poroceso:
La verdad no se que puede ser..el error es: Error en la peticion ajax..Requested JSON parse failed.
Clase ticket y detalle.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
//require_once ("includes/claseConexion.php");
require_once ("claseProducto.php");
class detalleTicket{
private $idp;
private $precio;
private $cantidad;
private $nombre;
private $subtotal;
public function __construct($id,$pu,$cant,$nom){
$this->idp=$id;
$this->precio=$pu;
$this->cantidad=$cant;
$this->nombre=$nom;
$this->subtotal= ($this->cantidad * $this->precio);
}
public function setSubtoal($sub){
$this->subtotal=$sub;
}
public function actualizaCantidad($nueva){
$this->cantidad=$this->cantidad + $nueva;
$sub= $this->cantidad * $this->precio;
$this->setSubtoal($sub);
}
}
class Ticket{
private $id;
private $detalle = array();
private $fecha;
private $hora;
private $total;
private $stm;
private $vuelto;
public function __construct($f,$h){
$this->fecha=$f;
$this->hora=$h;
}
public function agregaDetalle($d){
$this->detalle[] = $d;
}
public function setTotal($val){
$this->total=$val;
}
public function listarArt(){
return $this->detalle;
}
public function registrar(){
//me conecto a la bd
$conexion = Conexion::singleton_conexion();
try{
$conexion->beginTransaction();
$queryid="select comprobante from identificadores;";
$array=$conexion->query($queryid);
foreach($array as $a){
$id = $a['comprobante'];
}
$qnuevoid="UPDATE identificadores SET comprobante=".++$id.";";
$conexion->query($qnuevoid);
$queryticket="insert into despensa.ticket (idticket, fecha, total,vuelto) values (".$id.",'".$this->fecha."',56,88);";
$conexion->query($queryticket);
$conexion->commit();
}catch(Exception $e){
$conexion->rollBack();
echo "Fallo: " . $e->getMessage();
}
}
}
?>
El problema que tengo es que quiero listar los articulos añadidos al carrito al retornar a la pagina y me da un error de parseo de JSON: REQUESTED JSON PARSE FAILED.
Para lo mismo tengo un codigo jquery intermedio y un archivo de poroceso:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
$(document).ready(function() {
listarDetalle();
});
function listarDetalle(){
var accion="listar";
$.ajax({
type: "POST",
url: "../gestionweb/includes/php/procesoDetalle.php",
data: { "accion":accion},
dataType:'json',
error: function(){
alert("error petición ajax");
},
success: function(data){
console.log(data);
for (var i = 0; i < data.length; i++) {
var newRow =
"<tr>" +
"<td>" + data[i].idp + "</td>" +
"<td>" + data[i].nombre + "</td>"
"<td>" + data[i].marca + "</td>" +
"<td>" + data[i].cantidad + "</td>" +
"<td><input type='radio' id='"+data[i].idproducto+"' name='seleccion'/></td>"+
"</tr>";
$(newRow).appendTo("#ticket tbody");
} }
}).fail( function( jqXHR, textStatus, errorThrown ) {
if (jqXHR.status === 0) {
alert('Not connect: Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (textStatus === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (textStatus === 'timeout') {
alert('Time out error.');
} else if (textStatus === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error: ' + jqXHR.responseText);
}
});;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
header('Content-Type: application/json');
include ("../../models/claseTicket.php");
$ticket = new Ticket('12-12-12','12.10');
if (isset($_POST['accion'])){
if ($_POST['accion']="listar"){
$arrayarticulos=$ticket->listarArt();
echo json_encode($arrayarticulos);
}else if ($_POST['accion']="agregar"){
$id = $_POST['id'];
$precio = $_POST['precio'];
$cant=$_POST['cantidad'];
$nom=$_POST['nombre'];
$registro[] = new detalleTicket($id,$precio,$cant,$nom);
$ticket->agregaDetalle($registro);
}
}
?>
La verdad no se que puede ser..el error es: Error en la peticion ajax..Requested JSON parse failed.
Valora esta pregunta


0