2012-08-03 47 views
1

這個問題是參考我的另一個問題Auto complete not working。 這個問題仍然存在於我的代碼中,但我想這樣做的另一種方式。我想從另一個JavaScript函數調用我的web服務,並將服務返回的值傳遞給此自動完成函數,因爲當我嘗試將一些虛擬值傳遞給此jquery函數時它運行良好。我不確定它是不是在調用我的web服務。從javascript函數調用webservice並存儲結果

雖然現在我還編寫了另一個函數來調用我的服務,並獲得請求 -

 function SendRequest() 
    { 
    debugger; 
     SearchIssues.GetServerResponse(document.getElementById('ctl00_ContentPlaceHolder1_txtIssueNo').value, OnComplete, OnError, OnTimeOut); 
    } 
    function OnComplete(arg) 
    { 
     alert(arg); 
    } 
    function OnTimeOut(arg) 
    { 
     alert("timeOut has occured"); 
    } 
    function OnError(arg) 
    { 
     alert("error has occured: " + arg._message); 
    } 

在腳本經理踏歌我已經加入我的web服務的參考 -

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
     <Services> 
      <asp:ServiceReference Path="~/SearchIssues.asmx" /> 
     </Services> 
    </asp:ScriptManager> 

我有更新我的自動完成功能爲 -

$(function() { 
    debugger; 
     $(".tb").autocomplete({ 
      source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"] });}); 

這裏我已經傳遞了虛擬數據在源工作正常。

我的web服務的簽名是 -

[WebMethod] 
     [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
     public List<string> GetCompletionList(string prefixText) 
     {.... 
} 

但它仍沒有要求我的web服務,並返回一些JavaScript錯誤的 -

SearchIssues是不確定的

請幫助 謝謝

enter image description here

回答

0

這個工作對我來說

[WebMethod] 
      public static Array GetCompletionList(string code) 
    { 
    .....your code 
    } 


$.ajax({ 
       type: "POST", 
       url: "CompletionList.aspx/GetCompletionList", 
       data: '{"code1":"' +code1 + '"}', 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (c2) { 
        ....your code 
        }); 

}); 
0

1>我想打電話時,你應該有完整的命名空間

NameSpace.SearchIssues.GetServerResponse(document.getElementById('ctl00_ContentPlaceHolder1_txtIssueNo').value, OnComplete, OnError, OnTimeOut) 

2>您的服務類必須有[ScriptService]屬性。

3>測試你的相對URL爲服務

「〜/ SearchIssues.asmx」

+0

我刪除了命名空間......應它現在工作正常!!! – akhil 2012-08-03 12:12:06