Javascript 在 ajax/jquery 调用的成功函数之外使用变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5935968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use variable outside the success function from an ajax/jquery call
提问by DownDown
I have the following code
我有以下代码
var test;
$.ajax({
type: "GET",
url: "../views/person/controller.php?actor=person&action=checkAge",
data: "age=" + value,
success: function(msg){
console.log(msg);
test = msg;
},
});
Validate.fail(test);
Now the test var should give true of false like the console does say. But test var gives me undefined why?
现在测试变量应该像控制台所说的那样给出真假。但是 test var 给了我未定义的为什么?
回答by JohnP
var test; // <-- (1) This code runs first
$.ajax({ // <-- (2) Then this runs
type: "GET",
url: "../views/person/controller.php?actor=person&action=checkAge",
data: "age=" + value,
success: function(msg){
console.log(msg); //<-- (4) Finally this is run. IF your request is a success
test = msg;
},
});
Validate.fail(test); // <-- (3) This runs third
Look at the order in which the code runs. Your variable is simply not available at that point because it's running when the code is triggered via the callback
查看代码运行的顺序。您的变量此时根本不可用,因为它在通过回调触发代码时正在运行
回答by maple_shaft
Probably because Validate.fail(test) occurs immediately after the asynchronous call. Remember it is ASYNCHRONOUS, meaning it executes parallel to javascript running on your page.
可能是因为 Validate.fail(test) 在异步调用之后立即发生。请记住,它是异步的,这意味着它与在您的页面上运行的 javascript 并行执行。
回答by Julio Cesar Lopez Ocampo
enter code here var test;
$.ajax({
type: "GET",
async: false,
url: "../views/person/controller.php?actor=person&action=checkAge",
data: "age=" + value,
success: function(msg){
console.log(msg);
test = msg;
},
});
Validate.fail(test);
//Make your ajax function synchronous, set the json parameter "async: false", so javascript has to wait until test is assigned a value.
//让你的ajax函数同步,设置json参数“async:false”,这样javascript就得等到test被赋值了。
回答by Aylian Craspa
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var xm;
$("#txt").ajaxComplete(function(){
$('#txt').html(xm);
});
$("button").click(function(){
$.ajax({
url: 'DBresult_load.php',
dataType: 'html',
data: { }, //you can pass values here
success: function(result) {xm =result;}
});
});
});
</script>
</head>
<body>
<div id="txt"><h2>Let AJAX change this text</h2></div>
<button>Change Content</button>
</body>
</html>
Here is the solution for passing values to variable from Ajax request. Hope this helps.
这是将值从 Ajax 请求传递给变量的解决方案。希望这可以帮助。
回答by Atticus
What happens when you remove "var" before the term "test" when you declare it?
在声明“test”一词之前删除“var”会发生什么?
I'm not sure how the call back function is treated with jQuery , as it is wrapped within a few other extended methods.. But the reason i say leave var out in the declaration of the test
variable is that var
assigns test to be relative to the scope. If your callback is being treated in a certain way, you may lose the scope where test
is defined. You may want to drop the var
assignment and leave it as a global variable. Perhaps this will make it visible?
我不知道回调函数是如何使用jQuery处理,因为它是一些其他的扩展方法中包裹..但我之所以说假变量超出在声明test
变量是var
受让人测试是相对于范围。如果您的回调以某种方式被处理,您可能会失去test
定义的范围。您可能希望删除var
赋值并将其保留为全局变量。也许这会使它可见?
EDIT:: Didn't realize you were referencing the term within a function call after the async request -- i would suggest including the last statement within your callback.
编辑:: 没有意识到您在异步请求之后的函数调用中引用了该术语——我建议在您的回调中包含最后一个语句。
:)
:)