gliese581g gliese581g - 1 year ago 189
jQuery Question

Web2py AJAX call

I have a requirement where I want to make AJAX call from my html form but I cannot inherit my view from

I am trying a simple call using following code:

<input name="name" onclick="ajax('echo', [], 'this_div')"/>

<div id='this_div'></div>

In controller I have written following code:

def echo():

I am just checking if it hits the break point, but when I click it nothing happens.

Please let me know if I am missing anything.


I have added following js files in HTML:

<script src="{{=URL('static','js/modernizr.custom.js')}}" type="text/javascript" charset="UTF-8"></script>
<script src="{{=URL('static','js/web2py.js')}}" type="text/javascript" charset="UTF-8"></script>
<script src="{{=URL('static','js/jquery.js')}}" type="text/javascript" charset="UTF-8"></script>
<script src="{{=URL('static','js/val/dist/jquery.validate.js')}}" type="text/javascript"></script>
<script src="{{=URL('static','js/jquery-ui-1.10.3.custom.js')}}" type="text/javascript"></script>

But still it doesn't hit the break point.

Answer Source

The default layout.html typically includes web2py_ajax.html, which itself includes jQuery and web2py.js, both of which are needed for the ajax function to work. If you are not extending layout.html, then you will need to make sure jQuery and web2py.js are loaded on the page via other means (the best approach may depend on how this page is generated).

Also, the ajax call should be:

ajax('{{=URL("default", "echo")}}', [], 'this_div')

Always use the URL() function to generate internal URLs. Using just 'echo' makes the URL relative to the URL of the current page, which is not what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download