Hola: ¿Como se hace para llamar desde un js a un php...
Hola:
¿Como se hace para llamar desde un js a un php controller y que devuelva un json con un array de datos?
(tenía que hacer una pregunta)
Es muy sencillo y limpio, enviar y recoger datos con ajax pero no consigo que funcione dentro del facturascripts
js:
function getPropietario(idpropietario) {
var parametros = {
"codpropietario": idpropietario,
"accion": '3' // leer
};
$.ajax({
data: parametros,
url: 'index.php?page=propietarioEditar',
type: 'get',
success: function (response) {
if (response) {
var obj = jQuery.parseJSON(response);
if (obj.ERROR == 0) {
resultado = obj.QUERY;
if (resultado) {
$('nombre').val(resultado['Nombre']);
$('apellidos').val(obj.QUERY['Apellidos']);
}
}
}
},
fail: function (response) {
console.log(response);
}
});
}
php:
private propietarios = [];
(...)
protected function private_core() {
if (isset($_GET['accion'])) {
$nuevaaccion = $_GET['accion'];
switch ($nuevaaccion) {
case AL_ACCION_LEER :
$this->new_message('Propietario accionado-->' . $_GET['id']);
$codpropietario = $_GET['id'];
$this->leer_propietario($codpropietario);
header('Content-Type: application/json'); // esto lo vi en facturacion_base
echo json_encode($this->propietario);
break;
default:
break;
}
}
}
private function leer_propietario($codpropietario = 0) {
if ($codpropietario !== 0) {
$estepropietario = (new propietarios())->getByCodigo($codpropietario);
if ($estepropietario) {
foreach ($estepropietario as $key => $value) {
$this->propietario[$key] = $value;
}
}
}
}
Gracias.
08-03-2017 00:44:54
11-03-2017 00:19:36
127
[NeoRazorX]