我有一个在 flask 和 flask_restful 中实现的后端,并且有许多不同的路线。我的前端在另一个源上运行,这意味着我使用了 flask_curse 以允许我的前端向我的后端发送请求。
下面你可以看到我的应用程序的初始化:
app = Flask(__name__)
app.register_blueprint(check_routes_page)
CORS(app, supports_credentials=True)
这是我从前端调用的路线:
@check_routes_page.route(API_URL +API_VERSION +'check/email', methods=['POST'])
def check_email():
email = request.form['email']
user = User.query.filter(User.email == email).first()
if user:
return jsonify({'success':True}), 200
else:
return jsonify({'success': False}), 404
当我使用 Postman 发送请求时,一切正常。但是,当我从我的应用程序发送请求时,我总是返回 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 的请求时,客户端首先发送一个选项请求(返回代码 200),但实际请求仍然失败。
我感谢任何提示或建议。