【发布时间】:2019-12-18 18:28:10
【问题描述】:
我只是想使用 axios 向 Flask 发送一个 json post 请求。但是我在服务器控制台中得到“选项”,我知道这是预检请求。我发现如果我在 axios 的标头中使用 x-www-form-urlencoded 而不是 application/json,浏览器不会执行预检请求,所以我最终收到了 POST 请求。但是 POST 请求块(如您在下面的代码中所见)仍然没有受到打击。即使我在服务器中设置了访问控制允许来源,我仍然遇到 CORS 问题。这可能是什么问题?
//FLASK SERVER
@bp.route("/", methods=["GET", "POST"])
def recipes():
if request.method == "GET":
# show all the recipes
recipes = [
{'name': 'BURGER', 'ingredients': ['this', 'that', 'blah']},
{'name': 'CHICKEN'}
]
return jsonify(recipes)
elif request.method == "POST":
# save a recipe
print('SEE HEREEE'+ str(request.data))
print(request.is_json)
content = request.get_json()
print(content)
return jsonify(content), 201, {'Access-Control-Allow-Origin': '*', 'Access-Control-Request-Method': "*", 'Access-Control-Allow-Headers': "*"}
//FRONTEND
try{
let response = await axios({
method: "POST",
url: "http://localhost:5000/recipes/",
headers: {
"Content-Type": "*"
},
data: {"hello": "HI"}
});
console.log("RESPONSE HERE", response)
}catch(err){
throw new Error("ERROR", err)
}
【问题讨论】:
-
@ShanteshwarInde 不,不是!我的问题与 HTTPS 或凭据无关!!
-
你查过leo的答案了吗?
-
@ShanteshwarInde 重新启动浏览器??我正在使用 chrome,是的,我已经这样做了!