I am using jQuery to create textarea fields dynamically and then I am using the Map function in jQuery to get all of the values inside the textfields and create a list to Post to Flask. However, no matter what I try, Flask either returns nothing or I get a TypeError (TypeError: The view function did not return a valid response.) I have gone through this post and tried the various requests methods.
jQuery Post:
$("#check-btn").click(function() {
all_values = $('textarea[name="likes"]').map(function(){
return this.value;
}).get();
console.log(all_values);
//log example: ['test1\ntest2', 'test3\ntest4']
$.post("http://127.0.0.1:5000/likes", all_values)
.done(function(result) {
alert( "Data Loaded: ", result);
});
});
Dynamic textarea creation:
$(wrapper).append(
'<div id="column">\
<textarea name="likes" rows="20"></textarea>\
<br><a href="#" class="remove_field">Remove</a>\
<button class="add_field_button">Add Field</button>\
</div>'
);
Flask
@app.route('/likes', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
try:
data = request.data
print(data)
return data
except Exception as e:
print(e)
return e
# exception handling code
if request.method == 'GET':
return 'hi'