1

我正在使用 JQuery Ajax 从服务器获取脚本。

$.ajax({
    url: src,
    type: "GET",
    dataType: "script",
    timeout: transaction_timeout
}).done(function(){})
.fail(function() { alert("Error in server");})
.always(function() {Aegis.end();});

然后服务器返回一个回调函数,我需要覆盖它,以获得如下结果:

function callbackfunction(msg){
    var obj = jQuery.parseJSON(msg);
    var result = obj.result;
}

一切正常,直到我在回调函数中发出警报

function callbackfunction(msg){
    var obj = jQuery.parseJSON(msg);
    var result = obj.result;
    alert(result);
}

现在,如果用户在超时前没有点击确定,将会发生超时失败,并提示“服务器错误”。

同时,如果调用了回调函数,则意味着我的交易已成功完成。我该如何解决这个问题?

4

1 回答 1

3

一种解决方案是:

setTimeout(function(){alert(result);},0);

但最好的可能是根本不使用alert。你为什么不使用console.log

编辑:如果您想要以简单的方式显示消息,那并不难:

$('<div>').text('message!').css({position:'fixed', top:0, background:'yellow'})
.click(function(){$(this).remove()}).appendTo(document.body);

(点击后消息消失)

于 2013-05-29T10:45:15.000 回答