2013-05-14 64 views
0

我想從通過AJAX調用的Web服務文件中獲取用戶數據列表。這裏是我的代碼:我的jquery顯示這個錯誤?

 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 

<script type="text/javascript"> 
    var param; 
    var resultarr; 

    $(document).ready(function() {  
     param = document.getElementById('MainCT_dtvJobVac_PIC').value; 
     // Load countries then initialize plugin: 
     $.ajax({ 
      type: 'POST', 
      contentType: 'application/json;', 
      data: '{keyword:' + JSON.stringify(param) + '}', 
      dataType: 'json', 
      url: 'SvcADUser.asmx/GetADUserList', 
      success: function (result) { 
       //alert(result.d) 
       resultarr = result.d; 
      } 
     }) 

     // Initialize autocomplete with local lookup: 

     $('#MainCT_dtvJobVac_PIC').autocomplete({     
      source: resultarr 
     }); 
    }); 
</script> 

resultarr將輸出數組與此值:

[ "Administrator", "Guest", "krbtgt", "phendy" , "Genin" , "Hendra" , "andri" ] 

它拋出這個:

TypeError: this.source is not a function [Break On This Error] this.source({ term: value }, this._response());

什麼我需要在這裏解決?我在這掙了2天,有些幫助將不勝感激。

+2

這是一個不好的標題示例。請閱讀http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title – 2013-05-14 12:28:45

+0

「它扔我這個錯誤:'this.source({term:value},this。 _response());'「嗯,這不是一個錯誤。 – tnw 2013-05-14 12:30:10

+0

TypeError:this.source不是函數 [Break On This Error] \t this.source({term:value},this._response()); – NomNomNom 2013-05-14 12:31:16

回答

4

移動自動完成初始化了ajax成功回調中:

success: function (result) { 
    //alert(result.d) 
    resultarr = result.d; 
    $('#MainCT_dtvJobVac_PIC').autocomplete({ 
     source: resultarr 
    }); 
} 
+0

嗨,謝謝你的工作。再次非常感謝。 – NomNomNom 2013-05-14 12:34:52

+0

不客氣。自動完成之前使用未定義的源參數進行初始化。 – 2013-05-14 12:35:31

0

Ajax調用是異步的。讓我們看你的代碼:

$.ajax({ .... }); // (1) 

$('#MainCT_dtvJobVac_PIC').autocomplete({ ... }) // (2) 

自動完成初始化(2)調用服務(1)後發生,但它是不明的,如果AJAX請求成功,返回響應。很有可能您正在使用空數據或未定義的數據初始化自動填充 - 當連接速度很慢或出於某種原因失敗時,成功回調可能無法在設置自動填充(2)時執行。這樣做的正確方法是初始化AJAX回調中的自動完成,因爲那麼響應數據將保證存在:

$.ajax({ 
    type: 'POST', 
    contentType: 'application/json;', 
    data: '{keyword:' + JSON.stringify(param) + '}', 
    dataType: 'json', 
    url: 'SvcADUser.asmx/GetADUserList', 
    success: function (result) { 
     resultarr = result.d; 

     $('#MainCT_dtvJobVac_PIC').autocomplete({     
      source: resultarr 
     }); 
    } 
}) 
+0

謝謝你的解釋,它讓我對ajax有更多的理解,很多thanx。 – NomNomNom 2013-05-14 12:47:56