I'm writing a small app. Here is the code snippet.
Javascript:
<script type="text/javascript">
function filldata()
{
var sample = document.getElementById("filter-select").value;
jQuery.support.cors = true;
$.post("/test",{"filter_type":sample},function(data,status)
{
alert(data);
});
}
</script>
Flask Code:
@app.route('/test', methods=['POST'])
def test(str):
return str
It is giving me 500 internal error
.
When I run this in debug mode, it is saying:
test() takes exactly one argument(zero given)
Your code:
@app.route('/test',methods=['POST'])
def test(str):
return str
Expect a variable named str in input. With flask, when defining route the arguments of the function represent the url variables:
# the url have a parameter
@app.route('/user/<id>',methods=['GET'])
def test(id): # we get it here
return user.fromId(id)
To retrieve querystring you can use request.args
. And to get body request.data
or request.json
if you are sending json.
from flask import request
@app.route('/test',methods=['POST'])
def test():
return request.data
You need to parse argument from Flask's request object
from flask import request
@app.route('/test',methods=['POST'])
def test():
return request.form.get('filter_type')
See quickstart for more info
In your Flask code snippet, you have added an argument, but arguments are only sent if you change the URL to /test/<str:str>
. Since it is a POST request, you can access the data by using request.json
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.