0

I have a form which I need to add some variables to before sending it to the server which is python flask. I used the following code to send the form which works fine except when sending 2 dimensional array it will treat it as 1 dimensional in flask

javascript:

form = document.getElementById('calculator-form');
fdata = new FormData(form);
fdata.append('readings', plotData.readings);
$.ajax({
    url: "some/url/",
    type: 'POST',
    data: fdata,
    processData: false,
    contentType: false,
});

so if plotData.readings=[[1,2,3],[4,5,6]] I receive it in flask 1,2,3,4,5,6 and I don't always know the size of the array to reshape it from flask, is there a way to send so that the backend see it as 2 dimensional array?

2
  • 1
    Try fdata.append('readings', JSON.stringify(plotData.readings)); for sending, then you'll have to decode JSON, I'm not familiar with Python, but I'm sure there is a simple way to do it, JSON is a very popular format. Commented Oct 4, 2017 at 11:45
  • Thanks a lot, I thought there was a straight forward way without having to convert to json and converting back to list in python but this looks good for my current situation. Commented Oct 4, 2017 at 12:07

1 Answer 1

1

You can send plotData.readings as stringified JSON with JSON.stringify(plotData.readings) in Javascript. Then, in your Python app:

import json

json.loads(request.POST["readings"], encoding="utf-8") # Just an example
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.