这是来自webservice的返回xml列表:
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>
&#13;
然后我需要将它添加到以下jquery代码中的查询列表而不是本地值:
$('#query').typeahead({
local: ['alpha', 'allpha2', 'alpha3', 'bravo', 'charlie', 'delta', 'epsilon', 'gamma', 'zulu']
});
&#13;
我该怎么办?
答案 0 :(得分:0)
如果您需要创建/添加typeahead的本地属性,您可以执行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);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
另一种方法是使用source: function(query) {....代替本地。