Apologies, now looking back, I’ve noticed that your calling ajax.get, not $.get, d’oh.
Personally, i’d not send this request via GET, but as I have above, via POST. Does your ajax library have the ability to send a post request?
Based on my code, $.ajax returns a httpwebrequest object and performs a POST request with a form variable of “Query” set to the keywords you type in.
Then on completion (assuming it completes ok), it then automatically parses the response to json format, similar to what your doing.
If you don’t want to use POST, try and see if you can return the xmlhttprequest in your ajax.get query and within the callback function, eval it as you have and then call the associate_search.doSearch callback with the eval’d object.
<script type="text/javascript">
var associate_search =
{
this.last_query = '';
this.last_results = new Array();
this.ajax_request = null;
// takes the keywords (a string)
// and a callback (a function with
// one single param)
this.doSearch = function(keywords, callback)
{
// if the user is still typing,
// kill any previous searches
if(this.ajax_request != null)
{
// call the abort method on your xhr
this.ajax_request.abort();
}
// if the last query is the same
// as the current one, return
// the last set of results
if(this.last_query == keywords)
{
callback(this.last_results);
return null;
}
// set the last_query to the current keywords
this.last_query = keywords;
// store a local copy of "this",
// so it can be accessed
var em = this;
// create an ajax request and
// store the request in the
// ajax_request variable (to
// abort if need be
em.ajax_request = ajax.get('path to script', function(response)
{
var data = eval('(' + response + ')');
if(data)
{
em.last_results = data;
// clear the ajax_request
em.ajax_request = null;
// fire the callback with the results
callback(em.last_results);
}
});
// return the ajax_request
return em.ajax_request;
};
};
// the instance of associate_search
var as_instance = null;
$(document).ready(function()
{
$('#search').keyup(function()
{
// if the instance hasn't
// been defined, create it
if(as_instance != null)
{
as_instance = new associate_search();
}
// perform the search, passing
// the value of the text box
// and a callback function.
as_instance.doSearch($(this).val(), function(results)
{
// once we have got some results, so something with them!
});
});
});
</script>
Edit: Apologies, you’d need to change the keyup event I’d added to work with your own code.
A bit of advice… I used to use my own libraries and within about 10 minutes of using jQuery, I noticed that I was wasting my time. jQuery will save you a LOT of time in the long run.