从getJSON函数返回值

编程入门 行业动态 更新时间:2024-10-13 02:18:37
本文介绍了从getJSON函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

这是代码:

function getval(){ jQuery.getJSON('data.mtgox/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()); }); });

这不起作用:(

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

jQuery.getJSON('data.mtgox/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 }

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

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

非常感谢!!

推荐答案

以下是最终解决方案:

function getval( callback ){ jQuery.getJSON('data.mtgox/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!' ); } ); }); });

谢谢大家的帮助!!

更多推荐

从getJSON函数返回值

本文发布于:2023-11-26 02:28:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   返回值   getJSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!