i'm having trouble getting request data on server side. i'm newbie when comes web code som bear me. here's (simplified) code:
html
<form id="form" action="http://localhost:8080/report" enctype="multipart/form-data" method="post"> <div name="navigation-bar" id="navigation-bar"> <input type="submit" value="post" onclick="post()" /> </div> </form>
js
function post() { $.post("http://localhost:8080/report", {"id": 1}, function(data) { alert(data); }, "json"); }
py
@route('/report', method='post') def report(): payload = request.json return payload
payload none. correct way data when sending jquery?
setting datatype "json"
tells jquery how interptet response, doesnt't change format of request, still sent application/x-www-form-urlencoded
.
but bottle
's json handling works if request sent using correct content type of application/json
, if want use that, need manually set content type request , encode request data json:
function post() { $.ajax({ url: "/report", type: "post", data: json.stringify({"id": 1}), contenttype: "application/json", datatype: "json", success: function(data) { alert(data); } }); }
that works submission using javascript, normal form submission won't work, means should change submit button normal button or make sure oncklick returns false.
Comments
Post a Comment