ajax() function doesn't work properly (returns parent page)

121 views
Skip to first unread message

Dolgion Chuluunbaatar

unread,
May 2, 2012, 3:09:48 AM5/2/12
to web...@googlegroups.com
Hello,
I am trying to make the echo example work which is in the online book.
It works fine in my index function, but it doesn't work in any other function.
I use the same callback method in both views:

def echo():
   
return request.vars.keyword


and the methods (one is index, the other is called storeadmin) have identical code and views as well:

def index(): #as well as def: storeadmin():


   
return dict(
                form
= FORM (
                      INPUT
(
                          _name
='keyword',
                          _onkeyup
="ajax('echo', ['keyword'], 'target');"
                     
)
                 
),
                  target_div
=DIV(_id='target'))


the view is:


{{extend 'layout.html'}}


{{=form}}


<br />


{{=target_div}}

In index, the ajax call works as it should, but in storeadmin, I get the entire page pasted into the target div.
I put 2 screenshots of the pages for both methods to make it more clear.

do you have any idea why this could be?

Screenshot from 2012-05-02 15:07:04.png
Screenshot from 2012-05-02 15:07:19.png

Anthony

unread,
May 2, 2012, 12:15:03 PM5/2/12
to web...@googlegroups.com
Try going to http://127.0.0.1:8000/inventoryapp/default/index/ (note the trailing "/"), and you should see the same problem. On the storeadmin page, the problem should go away if you exclude the trailing "/" from the URL.

The problem is, in the call to ajax(), the URL is just 'echo'. Because it doesn't start with a "/", the browser interprets that as being relative to the current URL path, which it assumes ends after the last "/". If the current URL ends in a "/", the browser just adds "echo" to that and makes the Ajax call to the current URL (web2py will just see "echo" as a URL argument rather than a function). If you check the network traffic on the storeadmin example, you should see the Ajax call going to http://127.0.0.1:8000/inventoryapp/default/storeadmin/echo.

The best approach is to use the full URL in the Ajax call, using the URL() function:

_onkeyup="ajax(%s, ['keyword'], 'target');" % URL('default', 'echo')

We should probably change that in the book.

Anthony
Reply all
Reply to author
Forward
0 new messages