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

Showing 1-2 of 2 messages
ajax() function doesn't work properly (returns parent page) Wave Play 5/2/12 12:09 AM
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 (
="ajax('echo', ['keyword'], 'target');"

the view is:

{{extend 'layout.html'}}


<br />


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?

Re: ajax() function doesn't work properly (returns parent page) Anthony 5/2/12 9:15 AM
Try going to (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

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.