相当奇怪的问题是,除了 .ajax 函数本身,我不能在任何地方使用数据变量(ajax 调用返回的信息)。
我确信这是一个范围问题,但是它超出了我的范围,并且将不胜感激任何指示。
$('img#test').live('click', function(e) {
e.preventDefault();
var test = getPreviewImage();
alert(test); // This just gives undefined
});
function getPreviewImage()
{
var output;
var img_bg = $('div#preview-1 img:nth-child(1)').prop('src');
var img_fg = $('div#preview-1 img:nth-child(2)').prop('src');
$.ajax({
url: "/blah.php?v=12345,
}).done(function (data) {
alert(data); // This gives the correct response
output = data; // This should take the data value but still be in scope for the return statement below
});
return output;
}