I have a dropdown that I am using. Here is the 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>
Here is my flask view:
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)
Here is my jquery for the drop down. There is a event listenere for "clicking" of the submit button. I think the way I am hand
$('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;
});
When I run this it fails with:
400:Bad request