简体   繁体   中英

getting jquery post data null inside python flask

jquery ajax call

api_key=$('#acesskey').val();
    secret_key=$('#sacesskey').val();
    provider_name = $('#cname').val();
    $.ajax({
        type: "POST",
        data: {api_key:api_key,secret_key:secret_key,provider_name:provider_name},
        url: $SCRIPT_ROOT + "/login",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
            alert(data.status);
            if(data.status == "True")
            {
                custom_ready();         
            }
        }
    });

python flask code

provider_name=request.args.get('provider_name')
api_key=request.args.get('api_key')
secret_key=request.args.get('secret_key')
print provider_name
print api_key
print secret_key

output
None
None
None

request.args contains the query string parameters (like bar in /foo?bar=42 ). You are doing a POST request and want to look in request.form .

See here: http://flask.pocoo.org/docs/quickstart/#the-request-object

ajax call code

$.ajax({
    url: '/login',
    type: 'POST',
    data: {akey:akey,sakey:sakey,pname:pname},
    success: function(response){
        alert(response.status);
    }
});

HTML FORM

<form action="" method="POST">
</form>

Python code

akey = request.form['akey']
sakey = request.form['sakey']
pname = request.form['pname']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM