Click here to Skip to main content
Rate this: bad
good
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 23:01pm
Edited 15-Apr-13 23:05pm
v2
Comments
patel_vijay at 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 at 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
ערן לוי at 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
0 OriginalGriff 250
1 George Jonsson 175
2 Jochen Arndt 150
3 Kornfeld Eliyahu Peter 149
4 PIEBALDconsult 110
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
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