return value from getJSON function

◇◆丶佛笑我妖孽 提交于 2019-12-04 05:19:55

问题


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!!


回答1:


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!!




回答2:


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

});



回答3:


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



回答4:


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.....



来源:https://stackoverflow.com/questions/16020929/return-value-from-getjson-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!