2010-03-24 204 views
0

我在jQuery中調用的函數返回undefined。我檢查了這個函數,並且當我firebugged時它會返回正確的數據。jquery中的函數返回undefined

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

首先要注意:不要使用'eval'解析JSON:http://www.json.org/js.html – Matchu 2010-03-24 02:24:12

+0

感謝這個信息matchu! – steamboy 2010-03-24 04:53:51

回答

2

您正在通過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; 
} 
+0

感謝deceze和tvanfosson!現在我明白了。 但是,我將如何調用你的第一個方法的ajax回調? – steamboy 2010-03-24 02:57:03

+0

回調是'cb'。我已經使用'apply'顯示了它的調用。 – tvanfosson 2010-03-24 03:00:21

+0

我做了這個addToPlaylist($('#component_type')。val(),ui_item,0,cb);但仍然沒有找到。我不知道該怎麼做。非常感謝你的幫助。 – steamboy 2010-03-24 03:13:10

5

功能addToPlaylist沒有任何return。它使異步請求最終執行回調函數,該函數返回一些內容。原來的addToPlaylist函數很長時間並且在這種情況發生時返回,並且回調函數返回到nobody。

I.e. 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 
} 
1

我同意deceze。您需要做的是在成功函數中爲rendered_item執行必要的操作,而不是依賴從addToPlayList()返回的東西。