我在 localhost:5000 上托管了一个烧瓶端点,我试图从 localhost:3000 上托管的 React 应用程序向它发送请求。端点如下所示:
@app.route('/createUserSession', methods=["POST", "OPTIONS", "GET"])
@cross_origin()
def createUserSession():
response = Response()
response.headers["Access-Control-Allow-Origin"] = "http://localhost:3000"
response.headers["Access-Control-Allow-Methods"] = ["GET", "POST", "OPTIONS"]
response.headers["Access-Control-Allow-Credentials"] = "true"
return response
我使用 axios 从前端调用它,如下所示:
const api = axios.create({
withCredentials: false,
crossorigin: true
});
api.post('http://127.0.0.1:5000/createUserSession', queryString.stringify(params),
{headers: headers})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
我尝试了标头的各种排列,每次都收到 CORS 错误,告诉我不允许来源或凭据标头设置为 false。除了使用代理之外,还有其他解决方案吗?谢谢!