如何获取完整功能中的jQuery ajax数据? [英] How to get jQuery ajax data in the complete function?

查看:85
本文介绍了如何获取完整功能中的jQuery ajax数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是已知主题,解决方案之一是将呼叫更改为同步. 我仍然不清楚是否还有其他方法可以使它异步并在完整函数中获取数据? 示例函数在成功函数中创建了一个新的资产对象,我想在完整函数中获得对其的引用.

I know that this is known topic and one of the solutions is to change the call to be synchronous. Still it is not clear to me if there is any other way to do it async and obtain the data in the complete function? The example function creates a new asset object in success function and I want to obtain the reference to it in complete function.

        function getPresentation(item) {
        $.ajax({
            type: "GET",
            url: item.Url,
            success: function (data) {
                assets.push(new asset(item.Type, item.Url, data));
            },
            complete: function () {
                /// How to get here the reference for the newly created asset object?
                /// how to alert(asset)?
            },
            error: function (req, status, error) {
                alert('error');
            }
        });

    }

推荐答案

您可以简单地使用在complete事件中获得的jQXhr对象. complete事件的实际签名为complete(jqXHR, textStatus) 所以沿着

You can simply use the jQXhr object you get in the complete event. The actual signature of the complete event is complete(jqXHR, textStatus) so somethng along the lines of

complete:function(jqXHR,status)
{
    if(status == 'success' || status=='notmodified')
    {
        var asset = new asset(item.Type, item.Url, $.parseJSON(jqXHR.responseText))
    }
}

这篇关于如何获取完整功能中的jQuery ajax数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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