我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud) 我过去做过一些jQuery,但我完全坚持这个.我知道使用同步ajax调用的优点和缺点,但在这里它将是必需的.
加载远程页面(使用firebug控制),但不显示任何返回.
我应该做些什么让我的功能正常返回?
function getRemote() {
var remote;
$.ajax({
type: "GET",
url: remote_url,
async: false,
success : function(data) {
remote = data;
}
});
return remote;
}
Run Code Online (Sandbox Code Playgroud)