Codigo del webservice:
prueba1.php
<?php
$receive=file_get_contents('php://input');
$p = json_decode($receive);
$pp[0]["prueba0"] = $p["parametro1"];
$pp[0]["prueba1"] = "abc";
$pp[0]["ss"] = "zzz";
$p = json_encode($pp);
echo $p;
?>
Codigo JS:
function invocarAjax(url_ws, param_ws, funcion_ws, async_ws)
{
if(async_ws===undefined)
async_ws = false;
jQuery.ajax({
type: "POST",
timeout: 5000,
url: url_ws + "?" + "random=" + Math.floor(Math.random()*9999999999),
data: param_ws,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
async: async_ws,
success: function(response) {
window[funcion_ws](response, null);
},
error: function(xhr, ajaxOptions, thrownError) {
window[funcion_ws](null, xhr.responseText );
}
});
}
function getJSON(data_obj)
{
if (data_obj.d)
data_ok = data_obj.d;
else
data_ok = data_obj;
evalJson = eval("var varJSON = " + data_ok + ";");
return varJSON;
}
Código que invoca al webservice:
<script>
$(document).ready(function() {
$("#setear").click(function() {
url_ws = "prueba1.php";
param_ws = '{"parametro1": "5555", "enviar_todos": "S"}';
invocarAjax(url_ws, param_ws, "callbackConfigEmails", true);
});
});
function callbackConfigEmails(v, err)
{
if (err == null)
{
if (v[0].prueba1 != "")
{
alert("prueba0: " + v[0].prueba0);
alert("prueba1: " + v[0].prueba1);
}
} else
alert("ERROR: " + err);
}
</script>
OJO!!!!!
El codigo json_decode de jsonwrapper NO funciona, en su lugar, utilizar este:
function json_decode($json)
{
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++)
{
if (!$comment)
{
if (($json[$i] == '{') || ($json[$i] == '['))
$out .= ' array(';
else if (($json[$i] == '}') || ($json[$i] == ']'))
$out .= ')';
else if ($json[$i] == ':')
$out .= '=>';
else
$out .= $json[$i];
} else
$out .= $json[$i];
if ($json[$i] == '"' && $json[($i-1)]!="\\")
$comment = !$comment;
}
eval($out . ';');
return $x;
}
No hay comentarios:
Publicar un comentario