ajax异步回调函数中给外部变量赋值的问题探讨
代码如下:
<!doctype html> <head> <meta charset="utf-8"> </head> <body> <script src="http://localhost/UIBMSPHPProj/public/js/jquery-1.8.2.min.js"></script> <script> $(function(){ var str = "aaa"; $.post("http://localhost/UIBMSPHPProj/index.php/Index/abc",{},function(data){ //data = "bbb"; console.log(str);//aaa str = data; console.log(str);//"bbb" }); console.log(str);//"aaa" }); </script> </body> </html>
ajax回调函数异步的原因导致了在外部直接获取str的值时还是“aaa”,需要用“bbb”时,可以将用"bbb"的函数放入回调函数中调用。
相关推荐
stulen 2020-08-15
fengzhiwu 2020-08-01
liusarazhang 2020-06-28
xiaobaif 2020-06-14
lemonzhangyan 2020-06-13
Crazyshark 2020-06-13
weikaixxxxxx 2020-06-13
有梦就能实现 2020-06-10
shengnanonly 2020-06-03
oraclemch 2020-05-30
poplpsure 2020-05-27
fujuan000 2020-04-30
zuihaobushi 2020-04-30
luckymaoyy 2020-04-29
xiaoseyihe 2020-04-28
JackLang 2020-04-25
菇星獨行 2020-04-20
jyj00 2020-03-26