gpt4 book ai didi

Javascript Alert 干扰 Ajax

转载 作者:行者123 更新时间:2023-11-30 13:02:39 29 4
gpt4 key购买 nike

我正在使用 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);
}

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

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

最佳答案

一个解决方案是这样的:

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);

(点击后消息消失)

关于Javascript Alert 干扰 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16811785/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com