Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

Im Trying to return a value from callback function with no success.

Can you see what is wrong here??:

JavaScript
function getval( callback ){
    jQuery.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker?callback=?', function(data) {
        // We can't use .return because return is a JavaScript keyword.
        callback(data['return'].avg.value);
    });
}

$(function () {
        $(document).ready(function() {
        getval( function ( value ) {
            alert( 'Do something with ' + value + ' here!' );
        } );
    });

});

Here is JSFIddle link: http://jsfiddle.net/kf6qb/1/

Thank you very much!
Posted
Updated 15-Apr-13 22:05pm
v2
Comments
vijay__p 16-Apr-13 4:51am    
You can not use simple getJSON method for cross domain call, you have to use JSONP. checkout this link
http://stackoverflow.com/questions/6849802/jquery-getjson-works-locally-but-not-cross-domain
Prasad Khandekar 16-Apr-13 5:42am    
I suspect that the api you are trying to call is josnp ready.

1 solution

Hello,

I have created a samll sample which you can use for testing and should help you in achieving what you are trying.
HTML
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    getval(showAlert);
});

function getval( callback ) {
    $.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker')
	.done(function(data) {
		callback(data.return.avg.value);
	});
}

function showAlert(val) {
    alert( 'Do something with ' + val + ' here!' );
}
</script>
</head>
<body>
</body>
</html>

Regards
 
Share this answer
 
Comments
ערן לוי 16-Apr-13 7:44am    
Thank you!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900