$.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);

}

07-25 23:12