I'm trying to do a simple google-suggest like thing with django and ajax. I
wrote a view for creating the response and try to use it via an
XMLHttpRequest.
Here's my view:
def category_suggest(request):
> if request.method == "GET":
> return_categories = ''
> received_str = request.GET['str']
> found_categories =
> Category.objects.filter(name__istartswith=received_str) #i stands for
> incasesensitive
> if found_categories:
> for cat in found_categories:
> return_categories = return_categories + cat.name + "\n"
>
> print "returned str:"
> print return_categories
> return HttpResponse(str(return_categories), mimetype='text/plain')
> else:
> return HttpResponse('')
>
This view appears to work, because if I open the corresponding url in a
browser the data is displayed.
But it doesn't work via ajax: this is the javascript code:
//Gets the browser specific XmlHttpRequest Object
> function getXmlHttpRequestObject() {
> console.debug("GettingXMLHTTP");
> if (window.XMLHttpRequest) {
> return new XMLHttpRequest();
> } else if(window.ActiveXObject) {
> return new ActiveXObject("Microsoft.XMLHTTP");
> } else {
> alert("Your Browser Sucks!\nIt's about time to upgrade don't you
> think?");
> }
> }
>
> //Our XmlHttpRequest object to get the auto suggest
> var searchReq = getXmlHttpRequestObject();
>
> //Called from keyup on the search textbox.
> //Starts the AJAX request.
> function searchSuggest() {
> if (searchReq.readyState == 4 || searchReq.readyState == 0) {
> console.debug("start suggest...");
> var str = escape(document.getElementById('txtSearch').value);
> searchReq.onreadystatechange = handleSearchSuggest;
> var myDate = new Date();
> var myTime = myDate.getTime();
> searchReq.open("GET",
> 'http://127.0.0.1:8000/kalender/ajax/category_suggest/?str=' + str +
> "&time=" + myTime, true);
> console.debug("sending");
> searchReq.send(null);
> }
> }
>
> //Called when the AJAX response is returned.
> function handleSearchSuggest() {
> console.debug("Handling suggest");
> if (searchReq.readyState == 4) {
> console.debug("ReadyState is 4");
> var ss = document.getElementById('search_suggest')
> ss.innerHTML = '';
> var rstr = searchReq.responseText.split("\n");
> console.debug("Response:"+JSON.stringify(searchReq));
> for(i=0; i < rstr.length - 1; i++) {
> //Build our element string. This is cleaner using the DOM, but
> //IE doesn't support dynamically added attributes.
> var suggest = '<div ';
> suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
> suggest += 'class="suggest_link">' + rstr[i] + '</div>';
> ss.innerHTML += suggest;
> }
> }
> }
>
> //Click function
> function setSearch(value) {
> document.getElementById('txtSearch').value = value;
> document.getElementById('search_suggest').innerHTML = '';
> }
>
Every function is getting entered and firebug shows there's is a request
sendet to the django view but the responsetext is an empty string although
the django-view prints (for debugging purposes) the strings it should send
to the console.
I've asked many people who are really fit in javascript and noone could
find the error :( They told me it must be a django-related issue
So is it because of django and do You find the error?
Thanks
Schmidtchen
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/SmXwPWGtKnYJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.