问题描述
我在jQuery中调用的函数返回undefined。
函数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
{
//列出文件夹
folder_nam中的文件e = 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); //重新加载播放列表
console.log(rendered_item);
return rendered_item;
},
错误:function()
{
alert(更新时发生错误,请稍候再试一次);
$ b $('document')。ready(function()
{
addToPlaylist($(' #component_type')。val(),ui_item,0); //这个返回undefined
});
您通过AJAX提出请求,定义是异步的。这意味着你在AJAX请求完成之前从函数返回。事实上,你的return语句没有任何意义,因为它是从回调函数返回的,而不是你的 addToPlaylist
函数。
你有几个选择。首先,您可以使用AJAX请求的异步特性,并将回调传递到 addToPlaylist $>中。 c $ c>方法(就像你将匿名回调传递给ajax函数一样),并且拥有AJAX回调,调用该函数而不是返回。
函数addToPlaylist(component_type,add_to_pl_value, pl_list_no,cb)
{
... yada yada yada ...
$ .ajax({
...
成功:函数(数据){$ (cb){
cb.apply(this,rendered_item);
}
}
}); b $ b ...
}
其次,您可以添加选项 aSync:false
给ajax调用。这将强制AJAX调用同步运行(实质上它只是循环直到调用返回,然后调用您的回调)。如果这样做,则需要在回调中的 addToPlaylist
函数中捕获一个局部变量,并从回调中为其赋值(s)。在 addToPlaylist
函数的末尾,返回这个变量作为结果。
function addToPlaylist(component_type,add_to_pl_value,pl_list_no)
{
... yada yada yada ...
var result = null;
$ .ajax({
aSync:false,
...
成功:函数(数据){
...
结果= rendered_item;
}
});
return rendered_item;
}
The function I called inside jquery returns undefined. I checked the function and it returns correct data when I firebugged it.
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
});
You're making your request via AJAX, which by definition is asynchronous. That means you're returning from the function before the AJAX request completes. In fact, your return statement is meaningless as it returns from the callback function, not your addToPlaylist
function.
You have a couple of choices. The first one is better.
First, you can work with the asynchronous nature of the AJAX request and pass a callback into your addToPlaylist
method (much like you're passing in the anonymous callback to the ajax function) and have the AJAX callback, call that function instead of doing the return. That way your request completes asynchronously and doesn't lock up your browser while it's going on.
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 );
}
}
});
}
Second, you can add the option aSync: false
to the ajax call. This will force the AJAX call to run synchronously (essentially it just loops until the call returns then calls your callback). If you do that, you need to capture a local variable in your addToPlaylist
function inside the callback and assign the value(s) to it from the callback. At the end of the addToPlaylist
function, return this variable as the result.
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;
}
这篇关于jquery里面的函数返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!