我有一个带有4个引导按钮的客户端应用程序,想要读取每个按钮的状态。打开按钮将发送" L1 / 2/3 / 4ON"的适当POST请求。我是这样做的;
@app.route("/param=L3ON", methods=['POST'])
def handle_L3():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L2ON", methods=['POST'])
def handle_L2():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L1ON", methods=['POST'])
def handle_L1():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L4ON", methods=['POST'])
def handle_L4():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
**strong text**return 'OK'
我的javascript代码(在客户端)就像;
function ON(value) {
var request = new XMLHttpRequest();
if (value==="L1") {
request.open("POST","param=L1ON", true);
}else if (value==="L2") {
request.open("POST","param=L2ON", true);
}else if (value==="L3") {
request.open("POST","param=L3ON", true);
}else if (value==="L4") {
request.open("POST","param=L4ON", true);
}
request.send(null);
}
我正在寻找一种更好的方法,而不是为每个人制作单独的处理程序。有没有办法可以检查POST请求的某些部分,即@ app.route(" / param =",methods = [' POST']),然后我可以进一步通过使用" request"查找该请求中的相应字符来检查它是哪个请求。 ?
答案 0 :(得分:2)
您可以使用网址转换器:
@app.route('/param=<name>')
def handle(name):
if request.method == 'POST':
if name == 'L1ON':
#do something
elif name == 'L4ON':
#do something
return 'ok'