echo3
echo3

Reputation: 131

ajax post data from javascript to python

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')  

Upvotes: 1

Views: 3644

Answers (1)

corn3lius
corn3lius

Reputation: 5005

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

Upvotes: 3

Related Questions