具有返回值的getJSON,有人可以看看这里有什么问题吗? [英] getJSON with return value, Can someone take a look what is wrong here?

查看:70
本文介绍了具有返回值的getJSON,有人可以看看这里有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我试图从回调函数中返回一个值但没有成功。



你能看出这里有什么问题??:



<前lang =Javascript> 功能 getval (回调){
jQuery.getJSON(' http://data.mtgox.com/api/1 / BTCUSD / ticker?callback =?' function (data){
// 我们不能使用.return,因为return是一个JavaScript关键字。
callback(data [' return']。avg.value);
});
}

$( function (){
$( document )。ready( function (){
getval( function (value) {
alert(' 使用' + value + ' here!');
});
});

} );



这里是JSFIddle链接: http://jsfiddle.net/kf6qb / 1 /



非常感谢!

解决方案

function (){


document )。ready( function (){
getval( function (value){
alert(' 使用' + value + ' <执行某些操作span class =code-string> here!');
});
});

});



这里是JSFIddle链接: http://jsfiddle.net/kf6qb/1/



非常感谢!


您好,



我已经创建了一个小样本供您测试,可以帮助您实现目标。

 <   html  >  
< head >
< script 类型 = text / javascript src = http://code.jquery.com/jquery-1.9.1.js > < / script > ;
< script type = text / javascript >


Hello!

Im Trying to return a value from callback function with no success.

Can you see what is wrong here??:

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

});


Here is JSFIddle link: http://jsfiddle.net/kf6qb/1/

Thank you very much!

解决方案

(function () {


(document).ready(function() { getval( function ( value ) { alert( 'Do something with ' + value + ' here!' ); } ); }); });


Here is JSFIddle link: http://jsfiddle.net/kf6qb/1/

Thank you very much!


Hello,

I have created a samll sample which you can use for testing and should help you in achieving what you are trying.

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">


这篇关于具有返回值的getJSON,有人可以看看这里有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆