Alfonso
Alfonso

Reputation: 113

javascript json to python flask

I have a dynamic editable table. Once the user finished the edit with the submit button i need to send JSON stringify data to python flask.

This is the javascript that on button click create the alert with the JSON object... what should i add in the same function to send this data to python flask and render an Html template?

$(function () {
$("[id*=btnFirstGenerateJson").click(function () {
    var array1 = {};
    $("[id*=tblfirstTable] .DataRow").each(function () {

        var option = $(this).find('td').eq(1).find('select').children("option:selected")

        if(option.length>0){
            value = option.text();
        } else {
            value = $(this).find('td').eq(1).text().trim();
        }

        if (value!=undefined) {

            k = $(this).find('td').eq(0).text().trim();
            value = value.replace(/\t/g, '');
            value = value.replace('\n',' ');
            array1[k]=value
        }
    });
    alert(JSON.stringify(array1));
});

});

Upvotes: 1

Views: 117

Answers (1)

Karthick Mohanraj
Karthick Mohanraj

Reputation: 1658

Add this to your code snippet in your Javascript:

var xhr = new XMLHttpRequest();
var data = JSON.stringify(array1);
#Send the json data using the xhr variable
xhr.send(data);

Then in your flask application, you can retrieve the json files by using this code snippet inside your flask application:

@app.route('/flask_page',methods=[ "GET",'POST'])
def uploadLabel():
    #Retrieve the json file using request.files.get method of flask
    json_file = request.files.get('file')
    print(json_file)
    print(json_file.filename)
    json_file.save("./"+json_file.filename)

Upvotes: 1

Related Questions