I'm trying to figure out how to use jQuery AJAX with Python's Bottle module.
I've created a very basic page containing two buttons, one controlled by jQuery and the other one inside a form as a submit button. Both buttons just send a POST request to the a bottle server which should respond with a "Done!" message.
This is the code of the template:
<!DOCTYPE html>
<html>
<head>
<title>Test Template</title>
<!-- JQuery JS -->
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(document).ready(function() {
$('button').on('click', function(){
$.post("/home");
});
});
</script>
</head>
<body>
<button>Submit</button>
<form action="/home" method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" />
</form>
</body>
</html>
And this is the code of the bottle server:
from bottle import route, run, template
@route('/home')
def home():
return template('template')
@route('/home', method='POST')
def homePost():
return "Done!"
run(host='localhost', port=8080, debug=True)
run(reloader=True)
Why clicking the form button returns the message correctly but clicking on the jQuery one does nothing? Is jQuery blocking bottle's response somehow?
Thanks for your help.
Pablo