python - Ajax: Unable to send Json object to bottle webservice -
i trying understand how ajax call works.
i sending json object bottle python webservice url.
$.ajax({ type: "post", data: {"jstring": json.stringify(output)}, url: "http://localhost:8080/salesvolume" , contenttype: "application/json; charset=utf-8", datatype: "json", success: function(data){ $('#container').highcharts(data); }, error: function() { alert("something not ok") }, });
the above snippet ajax call. output
json object intend send server.
@app.post('/salesvolume') def salesvolume(db): jsonstring = request.forms.get('jstring') _jsonparams = json.loads(jsonstring) _studios = _jsonparams.studios ret = `some json` return json.loads(ret) app.run(server='paste', host='localhost', port=8080, debug=true, reloader=true)
and web service code snippet.
i status code: http/1.0 500 internal server error
i have been following bottle , jquery documentations im not able crack this. on greatful.
consider following things:
1) in js, change url simply: /salesvolume
.
2) in python, remove arg - db
salesvolume
function definition. or else might err (a 500
error):
typeerror: salesvolume() takes 1 argument (0 given) <myserverip> - - [30/jul/2015 13:31:27] "post /salesvolume http/1.1" 500 1328
3) check indentation. python is! guess
ret = json
and
return json.loads(ret)
needs indentation (they should inside salesvolume
function)
i wrote similar stuff , seems working:
python:
from bottle import * import json, requests @route('/') def athome(): return template('index') @route('/salesvolume', method="post") def salesvolume(): #your processings here... ret = '{"key":"val"}' return json.loads(ret) run(host='0.0.0.0', port=8093, debug=true, reloader=true)
index.tpl , js:
<html> <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <body> <button onclick=ajaxf()>click</button> </body> <script> function ajaxf(){ $.ajax({ type: "post", data: {"jstring": json.stringify("blah")}, url: "/salesvolume" , contenttype: "application/json; charset=utf-8", datatype: "json", success: function(data){ console.log('success'); console.log(data) }, error: function() { console.log("something not ok"); }, }); } </script> </html>
hope helps!
Comments
Post a Comment