Team
Team

Reputation: 257

How does Flask handle JSON?

I'm working on editable table using FLASK, JSON and Jquery

I have serialised form and sent via $.getJSON you can see in the bottom of my JS code:

Here is the JS code:

  $(function(){

 $('tbody').on('click', 'td', function() {

    displayForm( $(this) );

 });


function displayForm (cell) {

var column = cell.attr('name'),
    id = cell.closest('tr').attr('id'),
    cellWidth = cell.css('width')
    prevContent = cell.text(),
    form = '<form action="javascript: this.preventDefault"><input type="text" name="newValue" size= "4" value="'+prevContent+'"/><input type="hidden" name="id" value= "'+id+'" />'+'<input type="hidden" name="column" value="'+column+'"/></form>';

cell.html(form).find('input[type=text]')
.focus()
.css('width', cellWidth);

cell.on('click', function() {return false;});

cell.on('keydown', function(e) {

    if (e.keyCode == 13) {
        changeField(cell, prevContent);
    } else if (e.keyCode == 27) {
        cell.text(prevContent);
        cell.off('click');
    }

});
}

function changeField(cell, prevContent) {

cell.off('keydown');

var url = '/jsonurl?',
    input = cell.find('form').serialize();


$.getJSON(url+input, function(data){

    if (data.success)
        cell.html(data.value);
    else {
        alert('There was a problem updating the data.');
        cell.html(prevContent);
    }

});

cell.off('click');

}
});

and in the Processing side I use Flask to handle this data but when I edit the form and press enter to submit form it says: File "/Users/Team/Desktop/Flask/routes.py", line 72, in

jsonurl column = request.args.GET('column')

AttributeError: 'ImmutableMultiDict' object has no attribute 'GET'

What does this mean? It seems like I handle the JSON in the wrong way. Can anyone tell me what is the right way to handle this data?

Here are the codes in Processing side (which I think it is wrong and need your suggestion):

@app.route('/jsonurl')
def jsonurl():
    column = request.args.GET('column')
    id = request.args.GET('id')
    newValue = request.args.GET('newValue')

    g.db = connect_db()
    cur = g.db.execute('UPDATE customer SET column = newValue WHERE rowid=id')
    g.db.commit()
    g.db.close()
    return jsonify(success=True, value=newValue)

Upvotes: 0

Views: 337

Answers (1)

crazyzubr
crazyzubr

Reputation: 1082

Use lowercase 'get' instead of 'GET' with request.args below:

request.args.get('column')

.args contain GET parameters.

Upvotes: 2

Related Questions