1

I have a function with jquery getJSON and i need the return the result value back (to Use it somewhere else)

Here is the code:

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

$(function () {
    $(document).ready(function() {
    alert (getval());
    });

});

This is doesn't work :(

i know i can call external function from inside the getJSON function with the value like:

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

But i have to call the json function from another function because json return a dynamic value and i need to use it.

I hope it clear...

Thank you very much!!

Eran Levi
  • 877
  • 2
  • 12
  • 31
  • ajax is asynchronous, anything that needs the result needs to be ran in a callback that gets called after the ajax call completes. – Kevin B Apr 15 '13 at 17:18
  • possible duplicate of [How to return a value from a function that calls $.getJSON?](http://stackoverflow.com/questions/4200641/how-to-return-a-value-from-a-function-that-calls-getjson) – user229044 Apr 15 '13 at 17:21
  • 1
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Bergi Apr 30 '14 at 21:22

4 Answers4

9

Here is the final solution:

function getval( callback ){
    jQuery.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker', 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!' );
        } );
    });

});

Thanks everyone for your help!!

Eran Levi
  • 877
  • 2
  • 12
  • 31
3

You might try using a callback function:

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!' ) } );
    });

});
Kevin Boucher
  • 16,426
  • 3
  • 48
  • 55
  • Hi! just a small question.. when i use: "alert (getval( function ( value ) { alert( 'Do something here!' ) } ));" i get "undefined" can you see why?? Thank you very much! – Eran Levi Apr 15 '13 at 17:28
  • @EranLevi - I think Kevin didn't mean for that first "alert" to be there. The line should be more like this: `getVal(function(value) { alert('value is: ' + value); }`. – John S Apr 15 '13 at 17:45
  • Thank you! you probably meant to write: getVal(function(value) { alert('value is: ' + value); }); but the page is blank now :( can you see anything else?? Thanks man! – Eran Levi Apr 15 '13 at 17:54
  • @KevinBoucher - Hello and thank you!! i still can't see what is wrong in this simple code: [link](http://jsfiddle.net/kf6qb/1/) Thank you! – Eran Levi Apr 16 '13 at 07:47
1

Ajax calls are asynchronous, so you can't have the getVal() function return something. Whatever you need to do with the result, you have to do it in inside the callback function.

function getval() {
    jQuery.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker?callback=?', function(data) {
        // You have to use "data" here
        alert(data['return'].avg.value);
    });
}

$(function () {
    $(document).ready(function() {
        getval();
    });
});
John S
  • 21,212
  • 8
  • 46
  • 56
  • You right, but what if i need to refresh (get) the json call again and again? (json array values changing dynamically!) thank you! – Eran Levi Apr 15 '13 at 17:17
  • @EranLevi Then send the request again. – Kevin B Apr 15 '13 at 17:18
  • first.. Thanks! now, i can't send the request beause im calling from function No.1 to Function No.2, now here i cant call Function No.1 again (because it will call Func.No.2... :)... ) i really need to use the return value somehow in function No.2.. – Eran Levi Apr 15 '13 at 17:34
  • @EranLevi - You will have to move some of the code from the the one function to the other so it is inside the ajax callback function. I'd have to see the functions to comment further. – John S Apr 15 '13 at 17:48
1

Hi getJSON asynchronous call so its return undefind

so you need to fire ajax call pass with this args asnyc:false

Ex:

    function getCountrycodeJson(obj) {
     var code="";
        $.ajax({
         async: false,
          dataType : 'json',
         url: "url",
         type : 'GET',
         success: function(data) {
         for(var i in data){ 
//here do your logic and assign value for code varable   

          }
           }
      }});

        return code;
    }

this is working for me.....

Kathir
  • 4,359
  • 3
  • 17
  • 29