Click here to Skip to main content
12,395,352 members (67,327 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript jQuery
Hello!

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

Can you see what is wrong here??:

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 15-Apr-13 22:01pm
Updated 15-Apr-13 22:05pm
v2
Comments
patel_vijay 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

I have created a samll sample which you can use for testing and should help you in achieving what you are trying.
<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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100