I'm using this function on a jquery script:
function recogerEventos(id) {
var respuesta = $.ajax( {
type: 'GET',
url: 'eventos.php',
data: 'cmd=recoge&id='+id,
success: function(data) {
//alert(data);
return data;
},
error : function () {
return false;
}
}).responseText;
//alert("asdfksjfhajk"+respuesta);
return respuesta;
}
If I uncomment the alert(data) it shows the return string value, but when I uncomment the alert("asdffksd...") it shows only the random letters and no more, I need to send this 'respuesta' value to get string in other document.
If someone know how to get it, please, explain me, i'm desperate
Thanks to all, Carlos
You should provide a callback function to the recogerEventos function. So you can call that when the ajax request is finished.
function recogerEventos(id, callback, errorcallback) {
var respuesta = $.ajax( {
type: 'GET',
url: 'eventos.php',
data: 'cmd=recoge&id='+id,
success: callback,
error : errorcallback
});
}
Or you can do the update in the RecogerEventos directly:
$.ajax({
url: "test.html",
cache: false,
success: function(html){
$("#results").append(html);
}
An alternative is to set the async
option to false, which allows you to return the data directly from the call to ajax
. eg.
var respuesta = $.ajax({
url: 'eventos.php',
async: false,
...
}).responseText;
The downside to this approach is that it will block the browser whilst it waits for the request to complete.