Flask not getting any data from jQuery request data

后端 未结 4 2027
無奈伤痛
無奈伤痛 2020-12-15 23:35

I\'ve a handler for a URL,

@app.route(\"/\", methods=[\'POST\'])
@crossdomain(origin=\'*\')
def hello():
    ss=str(request.data)
    print ss
    return ss
         


        
4条回答
  •  太阳男子
    2020-12-15 23:49

    I have been working with similar functionality and after a bit of messing around with the ajax and python, this is what I came up with for python reading the ajax data

    JavaScript:

    var data = {
          data: JSON.stringify({
                            "value":'asdf'
                        })
       }
    };
    
    $.ajax({
       url:"/",
       type: 'POST',
       data: data,
       success: function(msg){
                  alert(msg);
                }
    })
    

    Python:

    from flask import json
    @app.route("/", methods=['POST'])
    def get_data():
        data = json.loads(request.form.get('data'))
        ss = data['value']
        return str(ss)
    

提交回复
热议问题