return value from getJSON function

后端 未结 4 1333

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.         


        
4条回答
  •  粉色の甜心
    2021-01-24 18:11

    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();
        });
    });
    

提交回复
热议问题