I am trying to load in my jquery autocomplete data from a text file, to reduce page load size. I am trying:
<script type="text/javascript">
$(document).ready(function(){
var practitioner_data = new Array();
$.get("/live_practitioners.txt", { },
function(data){
var data_array = new Array();
data_array = data;
$("#search_therapist_name").autocomplete(data_array, {minChars: 1, matchContains: true, mustMatch: true, selectFirst: false, formatItem: function(item) { return item.text; } }).result(function(event开发者_Python百科, item) { document[\'' . $str_form_name . '\'].counsellor_id.value=item.counsellor_id; });
});
});
</script>
Firebug shows me a first request to the txt file, and the data looks correctly formatted:
[{text:'Dr. Tania Abdulezer', counsellor_id:'877'}, {text:'Helen Acton', counsellor_id:'712'}]
However when I start typing into the text box, firebug is showing me a request being made, for each new letter typed, going to:
http://rscpplocal/%5B%7Btext:%27Dr.%20Tania%20Abdulezer%27,%20counsellor_id:%27877%27%7D,%20%7Btext:%27Helen%20Acton%27,%20counsellor_id:%27712%27%7D%5D?q=r&limit=10×tamp=1296041901116
Firstly, I am not sure why it is making a new request each time, I thought the point of loading it in from a string, to then supply as data, was so only one request was needed. Secondly, I have no idea why those subsequent requests, seem to think the file name, is, the data! From what I can tell, that first argument to autocomplete can be either the data, or a URL. I guess autocomplete thinks that my data is in fact a URL.
Any ideas what I am doing wrong please?
I've cracked it.
The data I was retrieving was of course only a string. Hence autocomplete considering it a URL, and making the new request.
I can turn my JSON string into an array with:
var data_array = eval(data);
精彩评论