0

I'm having an issue trying to get data from an ajax query in my flask project. The server receives a "NoneType".

Here is my ajax query:

function doChart(val){
    console.log(val);
    $.ajax({    
            url : baseURL + '/get-stat-data',
            dataType: 'json',
        data: JSON.stringify({
              item: val
            }),
            type : "POST",
        success:function(data){
            //alert(data.substr(1, data.length - 2));
            alert(data);      
        },

        error: function(XMLHttpRequest, textStatus, errorThrown) { 
            alert("Status: " + textStatus+", Error: " + errorThrown);  
        } 
    });

}

Flask code :

@application.route("/get-stat-data",methods=['GET', 'POST'])
def get_stat_data():
    if request.method == "POST":
        json = request.json
        #con = connect_db()
        print("data = "+json)

Error I get :

print("data = "+json)
TypeError: Can't convert 'NoneType' object to str implicitly

Thank you for your help

matcha-tea
  • 99
  • 10

1 Answers1

0

Put your data as

data: JSON.stringify({
       item: val
      }),

and mention a contentType: 'application/json;charset=UTF-8'

and then in Python -

postdata = request.json["item"]

This should do the trick.

Gaurav Ojha
  • 1,147
  • 1
  • 15
  • 37