-1
@app.route('/saveData',  methods=['POST'])
def saveData():
  print(request.data)

$(document).ready(function(){
 annotations = []

 anno.addHandler('onAnnotationCreated', function(annotation){
  annotations.push(annotation);
 });

 var doneButton = $('#done');
 doneButton.click(function(){  
  $.ajax({
         url: '/saveData',
         data: {annotations:annotations},
         type: 'POST',
         success: function(response) {
             console.log(response);
         },
         error: function(error) {
             console.log(error);
         }
     });
 });
});
I have an array of annotation objects in javascript. When I click the done button, I want to pass that array of objects to pass to the Flask function where I can store them in some database. How do I pass the array? When I click the done button currently, I get a console error 500 INTERNAL SERVER ERROR

The traceback is as follows: KeyError: 0 127.0.0.1 - - [17/Jul/2017 16:36:21] "POST /saveData HTTP/1.1" 500 - [2017-07-17 16:36:23,376] ERROR in app: Exception on /saveData [POST] Traceback (most recent call last): File "/home/user/Desktop/annotate/venv/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/home/user/Desktop/annotate/venv/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/user/Desktop/annotate/venv/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/user/Desktop/annotate/venv/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/home/user/Desktop/annotate/venv/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/user/Desktop/annotate/app.py", line 19, in saveData return(changed[0])

lenignes
  • 333
  • 1
  • 5
  • 17

1 Answers1

0

I'm not super familiar with flask but, the ajax call looks right. Based on this SO thread you may need to be using request.form instead of request.data

SimplyComplexable
  • 1,116
  • 11
  • 19
  • I tried `request.form` and I get a 500 internal server error! – lenignes Jul 17 '17 at 20:07
  • I also just noticed in the comments this function `request.get_json()`. Does that work any better? Also did you do the necessary imports `from flask import request`? – SimplyComplexable Jul 17 '17 at 20:10
  • Tried it. Doesn't work :( What I'm confused about is that in the route in python, how do I refer to the data being passed. Do I just refer to it as `data` so that if I'm trying to print the first object I would just do `request.form(data[0])` – lenignes Jul 17 '17 at 20:13
  • Also did you do the necessary imports `from flask import request`? It's a dictionary though, so you would reference it by whatever index you set, in your case all of the data would be under the index 'annotations' as set in your ajax method so `data['annotations']` – SimplyComplexable Jul 17 '17 at 20:15
  • the data is passed as an `ImmutableMultiDict` you can go `request.form.to_dict()` to transform it into a dict and extract the elements as such. – gold_cy Jul 17 '17 at 20:16
  • Expanding what @aws_apprentice just mentioned, something like: `data = request.form.to_dict()` `data['annotations'] //actual data you sent in post` – SimplyComplexable Jul 17 '17 at 20:17
  • @aws_apprentice I did `data = request.form.to_dict() \ print(data[0])` but I still keep getting `POST 500 INTERNAL SERVER ERROR` on the console – lenignes Jul 17 '17 at 20:23
  • it can be many things, first is your debug mode on? second try `return` instead of print. – gold_cy Jul 17 '17 at 20:26
  • @aws_apprentice updated my traceback error at the top – lenignes Jul 17 '17 at 20:37
  • `KeyError: 0` should give you a clue, look at the data first before you try and index it – gold_cy Jul 17 '17 at 20:42