Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery中的函数返回未定义的_Javascript_Jquery_Function_Undefined - Fatal编程技术网

Javascript jquery中的函数返回未定义的

Javascript jquery中的函数返回未定义的,javascript,jquery,function,undefined,Javascript,Jquery,Function,Undefined,我在jquery中调用的函数返回未定义的。我检查了该函数,当我对其进行Firebug时,它返回正确的数据 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

我在jquery中调用的函数返回未定义的。我检查了该函数,当我对其进行Firebug时,它返回正确的数据

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

函数
addToPlaylist
不返回任何内容。它发出一个异步请求,该请求最终执行一个回调函数,该函数返回一些内容。原始的
addToPlaylist
函数已经完成了很长一段时间,并且在发生这种情况时返回,回调函数不会返回给任何人

即,
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
}

我同意德克塞的观点。您需要做的是在success函数中为呈现的项目执行必要的操作,而不是依赖于从addToPlayList()获取内容。

您是通过AJAX发出请求的,根据定义,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;
}

第一个注意事项:不要使用
eval
解析JSON:谢谢deceze和tvanfosson!现在我明白了。但是我如何调用第一个方法的ajax回调呢?回调是
cb
。我用
apply
显示了它的调用。我做了这个addToPlaylist($('component_type').val(),ui_项,0,cb);但仍然返回未定义。我不知道怎么做。非常感谢您的帮助。如果您使用回调函数,您不应该期望函数返回任何内容——您应该将使用AJAX调用获得的值的代码放入回调函数中。