I have seen similar posts (which is how i have arrived at this current implementation) however i cannot get the below code to work. I want to:
- Receive input from the user (badge id)
- POST to the flask webserver via ajax
- process the badge id and send back the result
- receive the reply and process via javascript to be outputted via HTML onto the screen.
What is currently happening: 500 error TypeError: 'NoneType' object is not subscriptable.
When i use postman to submit a JSON to flask the reply works perfectly.
index.html
<!DOCTYPE html>
<html>
<head>
<title>App</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.4.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="{{ url_for('static', filename='form.js') }}"></script>
</head>
<body>
<div class="container">
<br><br><br><br>
<form class="form-inline">
<div class="form-group">
<input type="text" class="form-control" id="badge" name ="badgeinput" placeholder="Badge Scan..">
<button type="submit" class="btn btn-default">Submit</button>
<br>
<br>
<div class="container">
<span name="result" id="result">... </span>
</div>
</div>
</form>
<br>
</body>
</html>
form.js:
$(document).ready(function() {
$('form').on('submit', function(event) {
$.ajax({
data : {
badge_ID: $('input[name="badgeinput"]').val(),
},
type : 'POST',
url : '/process'
})
.done(function(data) {
document.getElementById('#result').innerHTML = data.result;
});
event.preventDefault();
});
});
Flask:
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process', methods=['POST'])
def process():
badge = request.json['badge_ID']
one = "Test"
new_badge_id = badge + one
return jsonify({'badge_ID' : new_badge_id})
if __name__ == '__main__':
app.run(host="localhost", port=4000, debug=True)