异步ajax调用完成后如何返回值?

发布于 2024-12-18 23:18:25 字数 1436 浏览 2 评论 0原文

可能的重复:
AJAX-响应数据未保存到全局范围?

内部使用 vimeo API 获取 vimeo 视频缩略图的函数:

    function getVimeoThumb(){
       var t = new Array();    
       $.ajax({
                'url' : 'http://vimeo.com/api/v2/video/' + id + '.json?callback=?',
                'dataType' : 'json',
                'async' : false,
                'type' : 'get',
                'success' : function(data) {

                t['img'] = data[0].thumbnail_large;
                t['src'] = 'vimeo';
                t['id'] = id;
            },
            'error' : function(){

                t['src'] = 'vimeo';
                t['img'] = '';
                t['id'] = id;
            }
        });
       return t;
     }

即使我将“async”值设置为false,它仍然不同步,因此变量“t”未定义。参考jquery.com:

"跨域请求和dataType:"jsonp"请求不支持 同步操作。”

还有其他解决方案吗?

更新
我需要它返回变量而不是调用其他函数。因为调用它的另一个函数等待它返回值。这是整个网站都使用的一种功能。

解决方案:(对于未来的访问者)
由于您无法对不同域进行异步调用,因此我创建了一个服务器端代码(在同一域上)来对 vimeo 进行 api 调用。流程如下:
ajax 调用本地服务器端脚本 ->对 vimeo api 的 api 调用 ->获取返回的任何内容 ->将其返回给 ajax 调用
这样,您就可以进行异步调用。希望这有帮助。祝你好运,感谢所有尝试回答这个问题的人!

Possible Duplicate:
AJAX- response data not saved to global scope?

Inside of function that gets the thumbnails of vimeo video using vimeo API:

    function getVimeoThumb(){
       var t = new Array();    
       $.ajax({
                'url' : 'http://vimeo.com/api/v2/video/' + id + '.json?callback=?',
                'dataType' : 'json',
                'async' : false,
                'type' : 'get',
                'success' : function(data) {

                t['img'] = data[0].thumbnail_large;
                t['src'] = 'vimeo';
                t['id'] = id;
            },
            'error' : function(){

                t['src'] = 'vimeo';
                t['img'] = '';
                t['id'] = id;
            }
        });
       return t;
     }

Even if I set "async" value to false, it is still not synchronous and hence variable "t" is undefined. Refer to jquery.com:

"Cross-domain requests and dataType: "jsonp" requests do not support
synchronous operation."

Any other solutions?

UPDATE:
I need it to return the variable and not to call some other function. Because another function which calls it waits for it to return the values. This is kind of function, which is used throughout the site.

SOLUTION: (for a future visitors)
Since you can't do async call to different domain, I've create a server side code (on the same domain) to do the api call to vimeo. The process is as follows:
ajax call to local server side script -> api call to vimeo api -> get whatever is returned -> return it to ajax call
With this, you can have async calls. Hope this helps. Good luck and thanks to everyone, who tried to answer this question!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

树深时见影 2024-12-25 23:18:25

JSONP 不是同步的,因为它基本上相当于...

该脚本的内容是...

cb12345({ /* ... json data ... */ });

所以你基本上有 2 个选择:

  • 拥抱异步代码
  • 尝试将异步代码转换为同步

拥抱异步

假设这个 $.ajax 代码在一个函数中(顺便说一句,$.ajax 确实毫无意义,使用 $.getScript 是一样的没有 jQuery 尝试表现得好像更多的事情正在发生)。

function GetVideoThumb(id, callback) {
    $.getScript('http://vimeo.com/api/v2/video/' + id + '.json', function(data){
        callback({ img: data[0].thumbnail_large, src: 'vimeo', id: id });
    });
}

这意味着每当您调用 GetVideoThumb 函数时,您都需要指定一个回调,该回调将在该函数起作用时进行处理。

假设您的代码看起来像这样...

$('#myelem').click(function() {
    var thumb = GetVideoThumb(1234);
    $(this).attr('src', thumb.img);
});

您可以将其更改为更像这样...

$('#myelem').click(function() {
    var that = this;
    GetVideoThumb(1234, function(thumb) {
        $(that).attr('src', thumb.img);
    });
});

JSONP isn't synchronous because it basically amounts to this...

<script src="http://vimeo.com/api/v2/video/12345.json?callback=cb12345"></script>

Where the content of that script is...

cb12345({ /* ... json data ... */ });

So you basically have 2 choices:

  • Embrace asynchronous code
  • Try to convert the async code to synchronous

Embracing Async

Say this $.ajax code is in a function (which on a side note $.ajax is really pointless, using $.getScript is the same dang thing without jQuery trying to act like more is going on).

function GetVideoThumb(id, callback) {
    $.getScript('http://vimeo.com/api/v2/video/' + id + '.json', function(data){
        callback({ img: data[0].thumbnail_large, src: 'vimeo', id: id });
    });
}

It means that whenever you call the GetVideoThumb function, you need to specify a callback that will handle whenever that works.

So say you had code that looked like this...

$('#myelem').click(function() {
    var thumb = GetVideoThumb(1234);
    $(this).attr('src', thumb.img);
});

You would change it to be more like this...

$('#myelem').click(function() {
    var that = this;
    GetVideoThumb(1234, function(thumb) {
        $(that).attr('src', thumb.img);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文