$.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.
Asked
Active
Viewed 774 times
1

random_user_name
- 25,694
- 7
- 76
- 115

Asmita
- 1,160
- 3
- 10
- 31
-
5Sometimes I'm amazed at how many things can go wrong in so little code... – elclanrs Oct 30 '12 at 05:52
-
What do you want to do with it? You'll have to store it somewhere or put your code that accesses it inside of the callback in order to access this scope, otherwise it's not possible. – Ian Oct 30 '12 at 05:52
-
Your problem is [variable scope](http://stackoverflow.com/a/500459/870729) – random_user_name Oct 30 '12 at 05:56
2 Answers
3
try declaring the currency variable outside the ajax function. you can set it a default value at first to prevent ambiguity if it returns 'undefined' (for debug purposes)
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);
}

CodeToad
- 4,656
- 6
- 41
- 53
-
if it logs 'debug', this is serving to notify us that there is a problem with the ajax call. Apparently there is signup required for this web service, so I did not try it. also, the 'parameters' parameter is undefined here. The code needs to run within the context of the rest of the application in order to be tested. – CodeToad Oct 31 '12 at 09:20
1
Im going to go out and guess this is a cross origin problem.
If I'm right you need to set your http server up as a proxy to openexhangerates.org.
Does your callback ever execute?

Roderick Obrist
- 3,688
- 1
- 16
- 17