2016-10-04 70 views
0

這是從一個web服務返回的XML列表:引導3預輸入從返回的web服務列表

public async Task<SelectResponse> AutoSearchService(string filter) 
    { 
     SelectResponse ResponseData = new SelectResponse(); 
     var Url = string.Format("{0}{1}", "serviceURL", filter); 
     var Client = new HttpClient(); 
     Client.BaseAddress = new Uri(Url.ToString()); 
     Client.DefaultRequestHeaders.Accept.Clear(); 
     Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml")); 
     var ResponseMessage = await Client.GetAsync(Url.ToString()); 
     if (ResponseMessage.IsSuccessStatusCode) 
     { 
      //Get Tickets from Banner 
      ResponseData = ResponseMessage.Content.ReadAsAsync<SelectResponse>().Result; 
     } 
     return ResponseData; 
    } 

,並獲得類似這樣的XML數據:

<SelectResponse> 
 
    <SelectResult> 
 
    <STVSBGIRECORDSELECT> 
 
     <STVSBGI_DESC>Holback Handelsskole</STVSBGI_DESC> 
 
    </STVSBGIRECORDSELECT> 
 
    <STVSBGIRECORDSELECT> 
 
     <STVSBGI_DESC>Los Gatos High Sch.-Los Gatos</STVSBGI_DESC> 
 
    </STVSBGIRECORDSELECT> 
 
    <STVSBGIRECORDSELECT> 
 
     <STVSBGI_DESC>Rich East High Sch.</STVSBGI_DESC> 
 
    </STVSBGIRECORDSELECT> 
 
    </SelectResult> 
 
</SelectResponse>

然後我需要將它添加到以下jQuery代碼中的查詢列表中,而不是本地值:

$('#query').typeahead({ 
 
     local: ['alpha', 'allpha2', 'alpha3', 'bravo', 'charlie', 'delta', 'epsilon', 'gamma', 'zulu'] 
 
    });

我應該怎麼辦?

回答

0

如果你需要創建/加入預輸入的本地地產STVSBGI_DESC標籤,你可以做的值:

// 
 
// Let's assume this is the xml as string you receive from your server 
 
// 
 
var str = '<SelectResponse>\ 
 
<SelectResult>\ 
 
<STVSBGIRECORDSELECT>\ 
 
<STVSBGI_DESC>Holback Handelsskole<\/STVSBGI_DESC>\ 
 
<\/STVSBGIRECORDSELECT>\ 
 
<STVSBGIRECORDSELECT>\ 
 
<STVSBGI_DESC>Los Gatos High Sch.-Los Gatos<\/STVSBGI_DESC>\ 
 
<\/STVSBGIRECORDSELECT>\ 
 
<STVSBGIRECORDSELECT>\ 
 
<STVSBGI_DESC>Rich East High Sch.</STVSBGI_DESC>\ 
 
<\/STVSBGIRECORDSELECT>\ 
 
<\/SelectResult>\ 
 
<\/SelectResponse>'; 
 

 

 
var localVar = ['alpha', 'allpha2', 'alpha3', 'bravo', 'charlie', 'delta', 'epsilon', 'gamma', 'zulu']; 
 

 

 
// 
 
// Use jQuery parseXML to convert it to an xml doc 
 
// 
 
var xmlDoc = $.parseXML(str); 
 

 
// 
 
// using jQuery iterate on tags adding value to your variable 
 
// 
 
$(xmlDoc).find('STVSBGI_DESC').each(function(index, value) { 
 
    localVar.push(value.textContent); 
 
}); 
 

 
console.log('the new value is: ' + localVar);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

另一種方法是使用source: function(query) {....,而不是本地的。