开发者

Returning values

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 va开发者_如何学JAVAlue, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜