Bueno, hoy me dio por aprender a trabajar con JSON
Leyendo y arreglando el codigo he llegado a algo mas o menos asi
archivo que genera el JSON, dev_stock.php
Código PHP :
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-type: application/json; charset=utf-8');
if(isset($_GET['codigo']) && !empty($_GET['codigo']))
{
//conexion a la BD
include('../conectar.php');
$qbusca=mysql_query("select * from inventario where COD_ART='".$_GET['codigo']."'");
if($rw=mysql_fetch_array($qbusca))
{
$pregunta = new stdClass();
$pregunta->codigo = $_GET['codigo'];
$pregunta->cantidad = $rw['can_existencia'];
$json = json_encode($pregunta);
echo $json;
}else{
$pregunta = new stdClass();
$pregunta->codigo = $_GET['codigo'];
$pregunta->cantidad = '0';
$json = json_encode($pregunta);
echo $json;
}
}
y este el archivo que hace la petición y que luego cargaría el los datos, pruebajson.php
Código PHP :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<h1></h1>
<h2></h2>
<script type="text/javascript">
$.getJSON('http://dominioexterno.cl/json/dev_stock.php?callback=?','codigo=TY815400X-O',{format: "jsonp"}, function(data) {
$("h1").html('CODIGO:'+data['codigo']);
$("h2").html('CANTIDAD:'+data['cantidad']);
});
</script>
</body>
</html>el archivo que genera el JSON anda bien, si entro directamente puedo ver el JSON que gernera, el problema al parecer esta en el $.getJSON. Revise por consola en chrome, y me muestra este error Uncaught SyntaxError: Unexpected token :
Ojala alguien me pueda decir en que me estoy equivocando, desde ya, gracias
