我有一个我正在使用的下拉列表。这是 HTML :
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="{{url_for('static',filename='dropdown_change.js')}}"></script>
<form action="{{url_for('select_ID')}}" method="POST">
<label for="input_ID">ID</label>
<input type="text" />
<label for="Node_Type">Node_Type</label>
<select id="dropdown_change">
<option value=1>Customer</option>
<option value=2>Phone</option>
<option value=3>ID_Card</option>
</select>
<input type='submit' value='Submit'>
</form>
这是我的瓶子视图:
import db_connect_test
from db_connect_test import Viz_Connector
from flask import Flask, request, session, redirect, url_for, render_template, flash,json,jsonify
import os
app = Flask(__name__)
@app.route('/',methods = ['GET','POST'])
def select_ID():
if request.method == 'POST':
ID=request.form['input_ID']
Node_Type = request.form['Node_Type']
data = Viz_Connector(ID,Node_type).get_data()
return render_template('dropdown.html',data=json.dumps(data))
return render_template('dropdown.html')
if __name__ == '__main__':
host = os.getenv('IP','0.0.0.0')
port = int(os.getenv('PORT',5000))
app.secret_key = os.urandom(24)
app.run(host=host,port=port)
这是我下拉的jquery。有一个事件监听&#34;点击&#34;提交按钮。我想我的方式
$('input[type=submit]').click(function() {
var ID = $("#input_ID").val();
var selectID = $("#dropdown_change").val();
$.ajax({
type: "POST",
url: "/select_ID",
data: {
ID: ID,
Node_Type: selectID //right way to pass the data to flask?
},
success: function(data) {
alert('SUCCESS: ' + data);
},
error: function(xhr, textStatus, errorThrown) {
document.getElementById('dropdown_change').selectedIndex = 0;
showMsg('ERROR: ' + errorThrown);
return false;
}
});
return false;
});
当我运行它时失败了:
400:Bad request
答案 0 :(得分:1)
您需要在将数据发送到服务器之前对其进行网址编码。此外,您的路由不会将POST
提取到路径/select_ID
。您应该发布到/
。稍微更流畅的方法是阅读jQuery中的action
属性。
HTML:
<form action="{{url_for('select_ID')}}" method="POST">
<label for="input_ID">ID</label>
<input id="input_ID" type="text" />
<label for="Node_Type">Node_Type</label>
<select id="Node_Type">
<option value=1>Customer</option>
<option value=2>Phone</option>
<option value=3>ID_Card</option>
</select>
<input type='submit' value='Submit'>
</form>
JavaScript的:
$('input[type=submit]').click(function() {
var input_ID= $("#input_ID").val();
var Node_Type= $("#Node_Type").val();
$.ajax({
type: "POST",
url: $(this).closest("form").attr("action"), // read the URL from the form attribute
data: $.param({ // use $.param() to convert from a JSON object to url-encoded
input_ID: input_ID,
Node_Type: Node_Type
}),
success: function(data) {
alert('SUCCESS: ' + data);
},
error: function(xhr, textStatus, errorThrown) {
document.getElementById('Node_Type').selectedIndex = 0;
showMsg('ERROR: ' + errorThrown);
return false;
}
});
return false;
});
注意:根据上面的评论,我对HTML ID和JavaScript变量进行了协调,以使用input_ID
和Node_Type
,因为这是服务器上的预期。
答案 1 :(得分:0)
尝试更改路线:
@app.route('/',methods = ['GET','POST'])
def select_ID():
到
@app.route('/select_ID',methods = ['GET','POST'])
def select_ID():