$.post("http://openexchangerates.org/", parameters,
function (data) {
var currencyData = eval('( '+data+')');
currency = currencyData["currency"];
}
);
// I want to access currency here. But I am unable to access it.
最佳答案
尝试在 ajax 函数之外声明货币变量。 如果它返回“未定义”(用于调试目的),您可以首先将其设置为默认值以防止歧义
function getCurrency(){
var currency = 'debug'; //TODO: remove this value once code works.
$.post("http://openexchangerates.org/", parameters,
function (data) {
var currencyData = eval('( '+data+')');
currency = currencyData["currency"];
}
);
console.log(currency);
}
关于javascript - 在 jquery ajax 调用之外访问变量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13133404/