Im trying to send a dict from javascript code to a python script via ajax through post. Heres the js:
function foo(){
context = {
'var1': val1,
'var2': val2
}
$.ajax({
url:'/pyfoo'
type: 'POST'
data: context,
success: function(){
...
}
});
I need to pull var1 and var2 from context in python but it doesn't come through. Any help would be appreciated.
I've tried a few thing in python:
def pyfoo():
data = json.loads('context')
1 Answer 1
From flask examples on their site :
def pyfoo() :
try:
data = json.loads(request.data)
print(data)
return "Success" 200
except ValueError:
error('Unable to parse JSON data from request.')
return "Error" 400
answered Mar 10, 2017 at 21:49
corn3lius
5,0053 gold badges33 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
json.loadsing the string you get in Python? How are you getting it?json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)jsonis essentially just a format for serializing a string-keyed hashmap as a string. Meaning you have to deserialize it when it gets where its going. It also seems that its not getting encoded by the client code, trydata: JSON.stringify(context),json.loads('context')? Why are you referencing the name of a client side variable in your server side code? What HTTP server are you using?