<分区>
Possible Duplicate:
How to return the response from an AJAX call from a function?
我曾尝试在 Google Chrome 的开发者控制台中进行调试,但无论如何我都无法获得我想要的结果。我想创建一个 javascript 函数,它将通过 AJAX 从外部文件返回一个值。我试过这个脚本:
var site = {
token: function(){
$.get('auth.php',function(data){
return data;
});
}
};
...当我调用 site.token()
函数时,它应该返回从 auth.php 获取的内容。但事实并非如此。它始终是 undefined
。我也试过这个:
var site = {
token: function(){
var token = null;
$.get('auth.php',function(data){
token = data;
});
return token;
}
};
但还是一样。我也通过这样做找到了解决方法:
var token = $.get('auth.php',function(data){
return data;
});
token.responseText;
有了这些代码,我现在可以得到预期的结果。但是当我已经把它放在一个函数中时:
var site = {
token: function(){
var token = $.get('auth.php',function(data){
return data;
});
return token.responseText;
}
};
...它又失败了。
*P.S.: I don't know maybe I'm just really stressed in programming and why I can't do it right. -_-"