jQuery 从 getJSON 函数返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16020929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:12:55  来源:igfitidea点击:

return value from getJSON function

javascriptjquerygetjson

提问by Eran Levi

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

我有一个带有 jquery getJSON 的函数,我需要返回结果值(在其他地方使用它)

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:

我知道我可以从 getJSON 函数内部调用外部函数,其值如下:

    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 jsonfunction from anotherfunction because json return a dynamic value and i need to use it.

但是我必须从另一个函数调用json函数,因为 json 返回一个动态值,我需要使用它。

I hope it clear...

我希望清楚...

Thank you very much!!

非常感谢!!

回答by Eran Levi

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

谢谢大家的帮助!!

回答by Kevin Boucher

You might try using a callbackfunction:

您可以尝试使用一个callback函数:

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

});

回答by kathir

Hi getJSON asynchronous call so its return undefind

你好 getJSON 异步调用所以它的返回undeffind

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

所以你需要用这个 args asnyc:false来触发 ajax 调用传递

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

这对我有用.....

回答by John S

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.

Ajax 调用是异步的,所以你不能让getVal()函数返回一些东西。无论您需要对结果做什么,都必须在回调函数内部进行。

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