javascript - using AJAX to get data from Bottlepy server -
javascript - using AJAX to get data from Bottlepy server -
i'm trying retrieve json info bottlepy server onto webpage. trying implement basic version first tried strings. nil seems happening. here code -
html(including js) -
<!doctype> <html> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <body> <script> function print() { $(document).ready(function(){ $.get('http://localhost:8080/check', function(result){ alert('success'); $('#main').html(result); }); }); } print(); </script></body> </html>
the python code -
from bottle import bottle, route, get,request, response, run, template app = bottle() @app.hook('after_request') def enable_cors(): response.headers['access-control-allow-origin'] = '*' # simple json test main page str = "hello" @route('/') #irrelevant question. used check server... def test(): homecoming template('file', str) @app.get('/check') def showall(): homecoming str run(host='localhost', port=8080)
what have access info on server? note : html separate file, , want code work irrespective of location of html.
also, if not possible, how can help of templates?
your problem stems confusion bottle app.
bottle creates default app whenever utilize @route
(more on this), , reuses default app implicitly on subsequent calls. default app behavior nowadays in many functions (including hook
, run
).
the point is:
app = bottle() # creates explicit app @route('/') # adds route default app @app.hook('after-request') # adds hook explicit app run(...) # runs default app, hook not used
to solve problem have 2 options:
remove mention of explicit app; always utilize app explicitlyi found using app explicitly made easier create sub-apps , much more clear in general going on.
new code:
import bottle bottle import response, template, run app = bottle.bottle() @app.hook('after_request') def enable_cors(): response.headers['access-control-allow-origin'] = '*' # simple json test main page str = "hello" @app.route('/') #irrelevant question. used check server... def test(): homecoming template('file', str) @app.get('/check') def showall(): homecoming str run(app=app, host='localhost', port=8080)
javascript jquery python ajax bottle
Comments
Post a Comment