Understand HTTP w/ AJAX in 15 minutes
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
sudo pip install bottle
Create a file called "app.py" (anywhere you like) and include this code:
import bottle
bottle.debug(True)
import sys, os, datetime
from bottle import route, get, post, put, delete, request, response, run, static_file, abort
@route('')
@route('/index.html')
def launch():
return static_file('index.html', root='')
@get('/devices')
def read_devices():
return {'name':'myiPad', 'uuid':1, 'model':'ipad 2 white', 'os':5.1}
# http://bottlepy.org/docs/dev/tutorial.html#quickstart-hello-world
run(host="0.0.0.0", reloader=True)
python app.py
To pull the data into a browser with javascript, create a file called "index.html" and include this:
<html>
<body></body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="application/javascript">
// http://api.jquery.com/jQuery.ajax/
$.ajax({
url: '/devices',
dataType: 'json',
type: 'GET',
success: function(json, status, xhr)
{
document.body.innerHTML = JSON.stringify(json);
},
error: function(xhr)
{
document.body.innerHTML = JSON.stringify(xhr);
}
});
</script>
</html>