Javascript 使AJAX“get”函数同步/如何获得结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10972892/
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
Make AJAX "get" function synchronous / how to get the result?
提问by Diolor
I'm experiencing a problem of $.get function. The url contains JSON
我遇到了 $.get 函数的问题。网址包含 JSON
my code:
我的代码:
xyz = null
$.get('http://www.someurl.com/123=json', function(data) {
var xyz = data.positions[0].latitude;
});
alert(xyz);
//some more code using xyz variable
I know that xyz
will alert a null result because the $.get
is asynchronous.
我知道这xyz
会提醒一个 null 结果,因为它$.get
是异步的。
So is there any way I can use the xyz
outside this get function?
那么有什么办法可以在xyz
外面使用这个get函数呢?
采纳答案by gdoron is supporting Monica
The real answer is NO, but you can use this:
真正的答案是否定的,但你可以使用这个:
function useXYZ(){
alert(xyz);
}
xyz = null
$.get('http://www.someurl.com/123=json', function(data) {
xyz = data.positions[0].latitude;
useXYZ();
});
回答by moribvndvs
get
is a shortcut. You can do the same, but synchronous, using:
get
是捷径。您可以使用以下方法执行相同但同步的操作:
var xyz = null
$.ajax({ url: 'http://www.someurl.com/123=json',
async: false,
dataType: 'json',
success: function(data) {
xyz = data.positions[0].latitude;
}
});
alert(xyz);
You'll have to declare the xyz
variable before the ajax call, though.
不过,您必须xyz
在 ajax 调用之前声明变量。
回答by hugomg
This is a common issue with Javascript. Javascript code must be written in continuation passing style. Its annoying but its something you can convert without thinking too much.
这是 Javascript 的常见问题。Javascript 代码必须以连续传递风格编写。它很烦人,但它是您无需考虑太多就可以转换的东西。
Basicaly, whenever we would have something like
基本上,每当我们有类似的东西时
var x = someSyncFunction(a, b, c);
//do something with x
console.log(x);
We can convert it into async code by making all the code after the function returns into a continuation function and turning x from a variable into a parameter of the continuation callback.
我们可以通过将函数返回后的所有代码都变成一个延续函数,并将 x 从一个变量变成一个延续回调的参数,从而将其转化为异步代码。
someAsyncFunction(a, b, c, function(x){
//do something with x;
console.log(x);
});
You have to watch out that its very easy to write confusing code. A good trick to keep in mind is taht you can make your own functions also receive callbacks. This allows them to be used by different function (just like normal sync helper functions that return a value can be used by different functions)
您必须注意编写令人困惑的代码非常容易。要记住的一个好技巧是,您可以让自己的函数也接收回调。这允许它们被不同的函数使用(就像返回值的普通同步辅助函数可以被不同的函数使用一样)
var getXyz = function(onResult){ //async functions that return do so via callbacks
//you can also another callback for errors (kind of analogous to throw)
$.get('http://www.someurl.com/123=json', function(data) {
var xyz = data.positions[0].latitude;
onResult(xyz); //instead of writing "return xyz", we pass x to the callback explicitely.
});
};
getXyz(function(xyz){ //this would look like "var xyz = getXyz();" if it were sync code instead.
console.log('got xyz');
});
The trick here is to change all return statements from the function into calls to the callback function. Think as if async function never returned and the only way to give a value back to someone is to pass that value to a callback.
这里的技巧是将函数中的所有返回语句更改为对回调函数的调用。想想好像异步函数从未返回,将值返回给某人的唯一方法是将该值传递给回调。
You might ask why there isnt an easier way to do all of this. Well, there is not, unless you use another language instead of Javascript (or at least something that lets you write async code in synchronous style but automatically compiles down to regular Javascript)
您可能会问为什么没有更简单的方法来完成所有这些工作。好吧,没有,除非您使用另一种语言而不是 Javascript(或者至少可以让您以同步样式编写异步代码但会自动编译为常规 Javascript)