gpt4 book ai didi

javascript - jquery 值不更新

转载 作者:行者123 更新时间:2023-11-28 11:17:25 24 4
gpt4 key购买 nike

function addrewarddb()
{
var rval="98";
$.post("db/mkreward.php",{
proid:getURLParameter("id")
},function(data){


rval="99";
alert(rval);
});
alert(rval);
return rval;

}

当这个函数执行时,函数返回 98 而不是 99。两个警报显示 98,然后是 99。如何使我的函数返回 99。我认为这里的问题是函数 returnig 在服务器页面 mkreward 的响应之前。 php.如何让函数在服务器响应后返回

最佳答案

$.post 调用是异步的。

这意味着该函数在 HTTP 请求完成之前返回。

然后,当 $.post() 调用完成时,它会更新变量并发出警报 99。

来自 jquery docs :

async Boolean Default: true By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the complete/success/error callbacks.

关于javascript - jquery 值不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14119400/

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