从应用程序(flask_corse)发送请求时始终返回400

2024-10-01 07:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个后端是在flask和flask_restful中实现的,并且有许多不同的路径。我的前端运行在另一个源上,这意味着我使用了flask_诅咒,以便让我的前端向后端发送请求。

下面您可以看到我的应用程序的初始化:

app = Flask(__name__)
app.register_blueprint(check_routes_page)
CORS(app, supports_credentials=True)

这是我从前端呼叫的路线:

^{pr2}$

当我用邮递员发送请求时,一切都很顺利。然而,当我从我的申请表发出请求时,我总是能得到400分。我还更改了内容类型,但没有成功。

这是我从我的申请中发出的请求。

checkMailAddress(email: string): boolean {

        let requestUrl = 'http://X/application/api/V0.1/check/email';
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        let body = JSON.parse('{"email":"' + email + '"}');
        let options = new RequestOptions({ headers: headers });
        let respo: any
        let us = this.http.post(requestUrl, body, options)
            .map((response) => response.json())
            .subscribe(
            function(response) {
                console.log("Success Response:" + response);
                respo = response;

            },
            function(error) {
                console.log("Error happened" + error);
            },
            function() {
                console.log("the subscription is completed");
                console.log(respo.status);
            }
            );
        return true;
    }

当我发送内容类型为Json的请求时,客户机首先发送一个option请求(返回代码200),但实际请求仍然失败。

我很感谢你的任何暗示或建议。


Tags: logapphttpflask类型内容emailresponse
2条回答

当这行失败时email = request.form['email'],然后flask发送400个错误请求。 使用request.isjson()检查请求主体是否为json或任何其他类型,如果不是,则使用data = request.getjson()将数据转换为json

尝试更改此行:

user = User.query.filter(User.email == email).first()

收件人:

user = User.query.filter(email=email).first()

相关问题 更多 >