Javascript request.get_data()、request.form等始终为空

Javascript request.get_data()、request.form等始终为空,javascript,ajax,flask,Javascript,Ajax,Flask,因此,我想使用flask向python脚本发送一个数组 以下是我的相关javascript: var blacklist = [1]; function getSents() { $.ajax({ type: "POST", url: "script.py", data: {'blacklist':blacklist}, dataType: 'json', success: function(response) { // do somet

因此,我想使用flask向python脚本发送一个数组

以下是我的相关javascript:

var blacklist = [1];
function getSents() {
 $.ajax({
    type: "POST",
    url: "script.py",
    data: {'blacklist':blacklist},
    dataType: 'json',
    success: function(response) {
        // do something
        },
    error: function (xhr, status, error) {
        console.log(error);
        }
});
}
在烧瓶里,我尝试了我找到的每一个请求属性。打印时,它总是给出一个空字节字符串

如果我使用

json.loads(request.data)
它引发错误,因为request.data为空

相关python代码:

from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
application = Flask(__name__)

@application.route('/', methods=['GET', 'POST'])
def something():
    blacklist = request.get_data()
    # do something

if __name__ == '__main__':
    application.run(debug=True)

尝试将contentType设置为“application/json”,并对数据属性进行字符串化。然后检查flask中的request.json

var blacklist = [1];
function getSents() {
 $.ajax({
    type: "POST",
    url: "script.py",
    data: JSON.stringify({'blacklist':blacklist}),
    contentType: "application/json",
    dataType: 'json',
    success: function(response) {
        // do something
        },
    error: function (xhr, status, error) {
        console.log(error);
        }
});
}
如果你正在寻找一个开始的地方,从这个应该工作

在app.py下

from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
application = Flask(__name__, static_url_path='/static')

@application.route('/', methods=['GET', 'POST'])
def something():
    if request.method == 'GET':
        return application.send_static_file('index.html')
    blacklist = request.json
    print(blacklist)
    return jsonify(blacklist)

if __name__ == '__main__':
    application.run(debug=True)
然后在/static/index.html下

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    </head>
    <body>
       <script>
        var blacklist = [1];
        function getSents() {
         $.ajax({
            type: "POST",
            url: "script.py",
            data: JSON.stringify({'blacklist':blacklist}),
            contentType: "application/json",
            dataType: 'json',
            success: function(response) {
                // do something
                console.log(response)
                },
            error: function (xhr, status, error) {
                console.log(error);
                }
        });
        }
        getSents();
        </script>
    </body>
</html>

测验
var黑名单=[1];
函数getSents(){
$.ajax({
类型:“POST”,
url:“script.py”,
数据:JSON.stringify({'blacklist':blacklist}),
contentType:“应用程序/json”,
数据类型:“json”,
成功:功能(响应){
//做点什么
console.log(响应)
},
错误:函数(xhr、状态、错误){
console.log(错误);
}
});
}
getSents();

似乎不起作用。不幸的是,与之前的行为相同。根据您的flask代码,我可能看到的唯一其他问题可能是:ajax请求中的url是script.py,而不是指向flask端点的url,您没有从flask路由返回响应,这将引发错误,如果您没有从其他地址提供内容,您将遇到cors问题。希望有帮助!