Re: Problem with Django and AJAX: Empty responsetext

2011-11-21 Thread Furbee
Disclaimer: I don't know if this is at all related, but I had an issue with
an AJAX object I created outside a function. Can you try this and let me
know if it works?

Change this:
var searchReq = getXmlHttpRequestObject();
to this:
searchReq = getXmlHttpRequestObject();

and move it into searchSuggest(), like:
function searchSuggest() {
searchReq = getXmlHttpRequestObject();
.
.
.
}

I can't explain this, but it tripped me up, because creating the AJAX
object globally, outside a function, didn't seem to work. I read an
explanation that made some sense, although it seemed like a bug, but it has
been too long and I can't remember exactly what the problem was. Keep me
posted.

Furbee

On Mon, Nov 21, 2011 at 1:10 PM, Schmidtchen Schleicher <
spiolli...@googlemail.com> wrote:

> 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 +
>> "=" + 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 = '> suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
>> suggest += 'class="suggest_link">' + rstr[i] + '';
>> 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 django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are 

Problem with Django and AJAX: Empty responsetext

2011-11-21 Thread Schmidtchen Schleicher
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 + 
> "=" + 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 = ' suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
> suggest += 'class="suggest_link">' + rstr[i] + '';
> 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 django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.