2

I've got a google-maps lookup with autocomplete on the client side - I'm trying to transfer the "place" object over to the server once the user selects it - I could parse it down client-side, but I figured it's easier to do server-side. I verified from the browser 'network' inspect that it is sending over the correct json object, but server side I can't get the right object. I've tried every permutation of request.* that I can find and either get None or a <module 'flask.json'> which I know isn't right.

Code:

function sendplace() {
    $('placebutton').click(function() {
        var add1 = place;
        console.log(place);
        $.ajax({
            url: '/new_place2',
            data: JSON.stringify(place),
            contentType: 'application/json;charset=UTF-8',
            type: 'POST',
            success: function(response) {
                console.log(response);
            },
            error: function(error) {
                console.log(error);
            }

and server-side:

@app.route('/new_place2', methods=['GET', 'POST'])
def new_place2():
    place = request.form.get('place')
    print "address: ", place
    return ("Success, (*&)er!")
Jeff Ericson
  • 61
  • 1
  • 9

1 Answers1

4

You should use request.get_json(). request.form is reserved for the mimetype application/www-form-urlencoded. If get_json() doesn't work look at request.data and ensure that you can run the following on it:

import json

data = json.loads(request.data)
data.get('place')

If that doesn't work you probably have a problem with your JSON

Tim Martin
  • 2,419
  • 1
  • 21
  • 25
  • Thanks - spent about two hours this morning looking - found lots of posts but not the one above... 'get_json()' worked. – Jeff Ericson May 17 '16 at 16:59