0

我在 jquery 中调用的函数返回未定义。我检查了这个函数,当我触发它时它返回了正确的数据。

function addToPlaylist(component_type,add_to_pl_value,pl_list_no) 
    {
        add_to_pl_value_split = add_to_pl_value.split(":");

        $.ajax({
            type: "POST",
            url: "ds/index.php/playlist/check_folder",
            data: "component_type="+component_type+"&value="+add_to_pl_value_split[1],
            success: function(msg)
            {
                if(msg == 'not_folder')
                {
                    if(component_type == 'video')
                    {
                        rendered_item = render_list_item_video(add_to_pl_value_split[0],add_to_pl_value_split[1],pl_list_no)
                    }

                    else if(component_type == 'image')
                    {
                        rendered_item = render_list_item_image(add_to_pl_value_split[0],add_to_pl_value_split[1],pl_list_no)
                    }
                }
                else
                {
                    //List files from folder
                    folder_name = add_to_pl_value_split[1].replace(' ','-');

                    var x = msg; // json 
                    eval('var file='+x); 

                    var rendered_item;

                    for ( var i in file )
                    {
                        //console.log(file[i]);
                        if(component_type == 'video')
                        {
                            rendered_item = render_list_item_video(folder_name+'-'+i,file[i],pl_list_no) + rendered_item;
                        }

                        if(component_type == 'image')
                        {
                            rendered_item = render_list_item_image(folder_name+'-'+i,file[i],pl_list_no) + rendered_item;
                        }
                    }
                }

                $("#files").html(filebrowser_list); //Reload Playlist

                console.log(rendered_item);

                return rendered_item;
            },
            error: function()
            {
                alert("An error occured while updating. Try again in a while");
            }
         })         
    }

$('document').ready(function()
{
    addToPlaylist($('#component_type').val(),ui_item,0); //This one returns undefined
});
4

3 回答 3

5

该功能addToPlaylist没有return任何作用。它发出一个异步请求,最终执行一个回调函数,该函数返回一些东西。原来的addToPlaylist函数早已完成并在这种情况发生时返回,回调函数返回到nobody。

success: function(msg) { }代码在不同的上下文中执行,并且比周围的addToPlaylist函数执行得晚。

试试这个看看它的实际效果:

function addToPlaylist() {
    $.ajax({
        ...
        success : function () {
            alert('second');  // comes after 'first'
            return null;      // returns to nobody in particular
        }
    });
    alert('first');      // comes before 'second'
    return 'something';  // can only return here to caller
}
于 2010-03-24T02:25:36.297 回答
3

您通过 AJAX 发出请求,根据定义,它是异步的。这意味着您在 AJAX 请求完成之前从函数返回。实际上,您的 return 语句毫无意义,因为它是从回调函数返回的,而不是您的addToPlaylist函数。

你有几个选择。第一个更好。

首先,您可以使用 AJAX 请求的异步特性并将回调传递给您的addToPlaylist方法(就像您将匿名回调传递给 ajax 函数一样)并使用 AJAX 回调,调用该函数而不是执行返回. 这样,您的请求将异步完成,并且在执行过程中不会锁定您的浏览器。

function addToPlaylist(component_type, add_to_pl_value, pl_list_no, cb )
{
    ...yada yada yada...
    $.ajax({
         ...
         success: function(data) {
            ...
            if (cb) {
               cb.apply(this, rendered_item );
            }
         }
    });
}

其次,您可以将选项添加aSync: false到 ajax 调用。这将强制 AJAX 调用同步运行(本质上它只是循环直到调用返回然后调用您的回调)。如果这样做,您需要addToPlaylist在回调内的函数中捕获一个局部变量,并从回调中为其分配值。在addToPlaylist函数结束时,将此变量作为结果返回。

function addToPlaylist(component_type, add_to_pl_value, pl_list_no )
{
    ...yada yada yada...
    var result = null;
    $.ajax({
         aSync: false,
         ...
         success: function(data) {
            ...
            result = rendered_item;
         }
    });

    return rendered_item;
}
于 2010-03-24T02:32:02.350 回答
1

我同意德塞兹。您需要做的是在成功函数中为render_item 执行必要的操作,而不是依赖于从addToPlayList() 中取回一些东西。

于 2010-03-24T02:31:46.970 回答