2017-07-26 79 views
2

我在我的Web表單.net應用程序中使用Select2 4.0.3。我正在嘗試使用web服務加載遠程數據,但它不像預期的那樣工作。使用Select2和Webservice加載遠程數據

創刊號現在面臨的是WebService的方法是沒有得到所謂的和我得到一個控制檯錯誤:

System.InvalidOperationException: Missing parameter: text. 
    at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) 
    at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) 
    at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 

所以,我想從Web服務調用

<WebMethod()> _ 
Public Function GetDataFromService() As String 

這樣做去除paremeter該方法被解僱,但仍然在select2中的項目沒有被填充(屏幕截圖已經存在enter image description here)。

有人能幫我弄清楚我在哪裏犯了一個錯誤。

下面是詳細信息:

選擇二碼在網絡表單:

$("#ddlEntity").select2({ 
 
     ajax: { 
 
      url: "Service/InvoiceHTMLService.asmx/GetDataFromService", 
 
      type: 'POST', 
 
      delay: 250, 
 
      params: { 
 
       contentType: 'application/json; charset=utf-8' 
 
      }, 
 
      dataType: 'json', 
 
      data: function (term, page) { 
 
       return { 
 
        text: term, 
 
       }; 
 
      }, 
 
      processResults: function (data, params) { 
 
       // parse the results into the format expected by Select2 
 
       // since we are using custom formatting functions we do not need to 
 
       // alter the remote JSON data, except to indicate that infinite 
 
       // scrolling can be used 
 
       params.page = params.page || 1; 
 

 
       return { 
 
        results: data.items, 
 
        pagination: { 
 
         more: (params.page * 30) < data.total_count 
 
        } 
 
       }; 
 
      }, 
 
      cache: true 
 
     }, 
 
     escapeMarkup: function (markup) { return markup; }, // let our custom formatter work 
 
     minimumInputLength: 1, 
 
     templateResult: formatRepo, // omitted for brevity, see the source of this page 
 
     templateSelection: formatRepoSelection // omitted for brevity, see the source of this page 
 
    });

WebService Method: 
    <WebMethod()> _ 
    Public Function GetDataFromService(text As String) As String 
     Return "{['id':1, 'text':'Test1'],['id':2, 'text':'Test2']}" 
    End Function 

回答

0

試試這個請

var fd = new FormData(); 
fd.append("text",term); 
ajax: { 
url: "Service/InvoiceHTMLService.asmx/GetDataFromService", 
type: 'POST', 
delay: 250, 
params: { 
    contentType: 'application/json; charset=utf-8' 
}, 
dataType: 'json', 
data: fd 
... 
0

我認爲你沒有爲這個select2創建模板。既然你正在使用templateResult和templateSelection它需要模板,或者你可以刪除這兩個選項。

供您參考:https://select2.github.io/examples.html#templating

更新時間:

他們已經改變查詢回調從

 data: function (term, page) { 
      return { 
       text: term, 
      }; 
     }, 

data: function (params) { 
    return { 
    q: params.term, // search term 
    page: params.page 
    }; 
}, 
+0

我移除了模板選項,還是兩者的問題依然存在,Web服務不會被調用,而不是數據正在被調用 回。 – user2561997

+0

oops響應應該是數組對象返回[{'id':1,'text':'Test1'}, {'id':2,'text':'Test2'}] –

+0

我能夠得到數據返回,但是我在帖子中提到的主要問題是,我無法獲取使用text參數調用的Web服務方法,如果我保留它爲空,它就可以工作。但我需要鍵入的文本作爲參數,以便我可以過濾數據。 – user2561997

相關問題